Skip to content

Commit f0fd592

Browse files
committed
Fix bug #81159: Object to int warning when using an object as a string offset
Closes GH-7167
1 parent 7bf930d commit f0fd592

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
(cmb, Nikita)
88
. Fixed bug #81163 (incorrect handling of indirect vars in __sleep).
99
(krakjoe)
10+
. Fixed bug #81159 (Object to int warning when using an object as a string offset).
11+
(girgias)
1012

1113
- Intl:
1214
. Fixed bug #72809 (Locale::lookup() wrong result with canonicalize option).

Zend/tests/bug81159.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #81159: Object to int warning when using an object as a string offset
3+
--FILE--
4+
<?php
5+
6+
$s = 'Hello';
7+
$o = new stdClass();
8+
try {
9+
$s[$o] = 'A';
10+
} catch (\Throwable $e) {
11+
echo $e->getMessage(), "\n";
12+
}
13+
try {
14+
var_dump($s[$o]);
15+
} catch (\Throwable $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
?>
19+
--EXPECT--
20+
Cannot access offset of type stdClass on string
21+
Cannot access offset of type stdClass on string

Zend/tests/offset_string.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ string(1) "i"
6868
Warning: String offset cast occurred in %s on line %d
6969
string(1) "S"
7070
Cannot access offset of type resource on string
71-
72-
Warning: Object of class stdClass could not be converted to int in %s on line %d
7371
Cannot access offset of type stdClass on string
7472
Cannot access offset of type array on string
7573
Done

Zend/zend_execute.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type
13841384
return offset;
13851385
}
13861386
zend_illegal_string_offset(dim);
1387-
break;
1387+
return 0;
13881388
}
13891389
case IS_UNDEF:
13901390
ZVAL_UNDEFINED_OP2();
@@ -1399,7 +1399,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type
13991399
goto try_again;
14001400
default:
14011401
zend_illegal_string_offset(dim);
1402-
break;
1402+
return 0;
14031403
}
14041404

14051405
offset = zval_get_long_func(dim);
@@ -2384,7 +2384,8 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
23842384
return;
23852385
}
23862386
zend_illegal_string_offset(dim);
2387-
break;
2387+
ZVAL_NULL(result);
2388+
return;
23882389
}
23892390
case IS_UNDEF:
23902391
ZVAL_UNDEFINED_OP2();
@@ -2401,7 +2402,8 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
24012402
goto try_string_offset;
24022403
default:
24032404
zend_illegal_string_offset(dim);
2404-
break;
2405+
ZVAL_NULL(result);
2406+
return;
24052407
}
24062408

24072409
offset = zval_get_long_func(dim);

0 commit comments

Comments
 (0)