Skip to content

Commit fd4bbc0

Browse files
committed
Merge branch 'PHP-8.5' into PHP-8.6
2 parents ac9a9cd + 01e431b commit fd4bbc0

8 files changed

Lines changed: 199 additions & 5 deletions

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ PHP NEWS
5252
ZEND_ADD_ARRAY_ELEMENT handler). (Jorg Sowa, ndossche)
5353
. Fixed bug GH-25253 (JIT reads parent of an unlinked class).
5454
(lazerg, mlenarciak)
55+
. Fixed JIT ignoring leading-numeric string offsets in isset(), empty() and
56+
??. (Jorg Sowa)
5557

5658
- OpenSSL:
5759
. Fixed bug GH-25172 (TLS session reuse shares request-lifetime callback

‎Zend/Optimizer/zend_inference.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5300,7 +5300,9 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
53005300
case ZEND_ISSET_ISEMPTY_DIM_OBJ:
53015301
return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_THROWING_KEY);
53025302
case ZEND_FETCH_DIM_IS:
5303-
return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_THROWING_KEY);
5303+
return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_THROWING_KEY)
5304+
/* "Illegal string offset" warning for leading-numeric string offsets */
5305+
|| ((t1 & MAY_BE_STRING) && (t2 & MAY_BE_STRING));
53045306
case ZEND_CAST:
53055307
switch (opline->extended_value) {
53065308
case IS_LONG:

‎ext/opcache/jit/zend_jit_helpers.c‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,11 +1262,27 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_str_is_helper(zend_string *str, zva
12621262
switch (Z_TYPE_P(dim)) {
12631263
/* case IS_LONG: */
12641264
case IS_STRING:
1265-
if (IS_LONG == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, false)) {
1266-
goto out;
1265+
{
1266+
bool trailing_data = false;
1267+
/* Allow errors to warn on leading-numeric strings, like the VM */
1268+
if (IS_LONG != is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset,
1269+
NULL, /* allow errors */ true, NULL, &trailing_data)) {
1270+
ZVAL_NULL(result);
1271+
return;
12671272
}
1268-
ZVAL_NULL(result);
1269-
return;
1273+
if (UNEXPECTED(trailing_data)) {
1274+
if (!(GC_FLAGS(str) & IS_STR_INTERNED)) {
1275+
GC_ADDREF(str);
1276+
}
1277+
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
1278+
if (!(GC_FLAGS(str) & IS_STR_INTERNED) && UNEXPECTED(GC_DELREF(str) == 0)) {
1279+
zend_string_efree(str);
1280+
ZVAL_NULL(result);
1281+
return;
1282+
}
1283+
}
1284+
goto out;
1285+
}
12701286
case IS_DOUBLE:
12711287
offset = zend_dval_to_lval_silent(Z_DVAL_P(dim));
12721288
goto out;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
JIT FETCH_DIM_IS: string offsets given as strings behave like the VM
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.jit=function
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
function test(string $s, string $k) {
13+
return $s[$k] ?? "default";
14+
}
15+
16+
foreach (["1", "-1", "1 ", " 1", "1x", "-1x", "9x", "x", "", "1.5", "1.5x", "99999999999999999999"] as $k) {
17+
echo var_export($k, true), ": ";
18+
var_dump(test("abc", $k));
19+
}
20+
?>
21+
--EXPECTF--
22+
'1': string(1) "b"
23+
'-1': string(1) "c"
24+
'1 ': string(1) "b"
25+
' 1': string(1) "b"
26+
'1x':
27+
Warning: Illegal string offset "1x" in %s on line %d
28+
string(1) "b"
29+
'-1x':
30+
Warning: Illegal string offset "-1x" in %s on line %d
31+
string(1) "c"
32+
'9x':
33+
Warning: Illegal string offset "9x" in %s on line %d
34+
string(7) "default"
35+
'x': string(7) "default"
36+
'': string(7) "default"
37+
'1.5': string(7) "default"
38+
'1.5x': string(7) "default"
39+
'99999999999999999999': string(7) "default"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
JIT FETCH_DIM_IS: leading-numeric string offset conversion may throw
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.jit=function
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
set_error_handler(function ($errno, $errstr) {
13+
throw new Exception($errstr);
14+
});
15+
16+
function make(int $n) {
17+
return str_repeat("abc", $n);
18+
}
19+
20+
function consume($v) {
21+
echo "unreachable\n";
22+
}
23+
24+
function test(string $s) {
25+
consume($s["1x"] ?? "default");
26+
}
27+
28+
try {
29+
test(make(2));
30+
} catch (Exception $e) {
31+
echo $e::class, ": ", $e->getMessage(), "\n";
32+
}
33+
?>
34+
--EXPECT--
35+
Exception: Illegal string offset "1x"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
JIT FETCH_DIM_IS: leading-numeric string offset may throw between call arguments
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.jit=function
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
set_error_handler(function ($errno, $errstr) {
13+
throw new Exception($errstr);
14+
});
15+
16+
function make(int $n) {
17+
return str_repeat("abc", $n);
18+
}
19+
20+
function consume($a, $b) {
21+
echo "unreachable\n";
22+
}
23+
24+
function test(string $s, string $t) {
25+
consume($t, isset($s["1x"][0]));
26+
}
27+
28+
try {
29+
test(make(2), make(3));
30+
} catch (Exception $e) {
31+
echo $e::class, ": ", $e->getMessage(), "\n";
32+
}
33+
?>
34+
--EXPECT--
35+
Exception: Illegal string offset "1x"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
JIT FETCH_DIM_IS: leading-numeric string offset warning may release the string
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.jit=function
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
set_error_handler(function ($errno, $errstr) {
13+
echo "Warning: $errstr\n";
14+
$GLOBALS['s'] = null;
15+
});
16+
17+
function make(int $n) {
18+
return str_repeat("abc", $n);
19+
}
20+
21+
function test() {
22+
global $s;
23+
return $s["1x"] ?? "default";
24+
}
25+
26+
$s = make(2);
27+
var_dump(test());
28+
?>
29+
--EXPECT--
30+
Warning: Illegal string offset "1x"
31+
string(7) "default"

‎ext/opcache/tests/opt/dce_028.phpt‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
DCE must not remove FETCH_DIM_IS of a nested isset()/empty() with a leading-numeric string offset
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.optimization_level=-1
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
set_error_handler(function ($errno, $errstr) {
13+
throw new Exception($errstr);
14+
});
15+
16+
function test_isset(string $s) {
17+
$x = isset($s["1x"][0]);
18+
}
19+
20+
function test_empty(string $s) {
21+
$x = empty($s["1x"][0]);
22+
}
23+
24+
foreach (['test_isset', 'test_empty'] as $f) {
25+
try {
26+
$f("abc");
27+
} catch (Exception $e) {
28+
echo $e::class, ": ", $e->getMessage(), "\n";
29+
}
30+
}
31+
?>
32+
--EXPECT--
33+
Exception: Illegal string offset "1x"
34+
Exception: Illegal string offset "1x"

0 commit comments

Comments
 (0)