Skip to content

Commit b1fea43

Browse files
jorgsowandossche
andcommitted
Optimizer: null array keys may throw
Null keys emit a deprecation since PHP 8.5, which an error handler can turn into an exception. Add MAY_BE_NULL to MAY_BE_THROWING_KEY. Fixes GH-25269 Co-authored-by: Nora Dossche <7771979+ndossche@users.noreply.github.com>
1 parent 532fcd1 commit b1fea43

13 files changed

Lines changed: 341 additions & 2 deletions

‎Zend/Optimizer/zend_inference.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4935,7 +4935,7 @@ ZEND_API zend_result zend_ssa_inference(zend_arena **arena, const zend_op_array
49354935
/* }}} */
49364936

49374937
/* Key types whose conversion to an array offset may emit a diagnostic */
4938-
#define MAY_BE_THROWING_KEY (MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)
4938+
#define MAY_BE_THROWING_KEY (MAY_BE_NULL|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)
49394939

49404940
ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa, uint32_t t1, uint32_t t2)
49414941
{
@@ -5273,7 +5273,7 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
52735273
case ZEND_INIT_ARRAY:
52745274
return (opline->op2_type != IS_UNUSED) && (t2 & MAY_BE_THROWING_KEY);
52755275
case ZEND_ADD_ARRAY_ELEMENT:
5276-
return (opline->op2_type == IS_UNUSED) || (t2 & (MAY_BE_NULL|MAY_BE_THROWING_KEY));
5276+
return (opline->op2_type == IS_UNUSED) || (t2 & MAY_BE_THROWING_KEY);
52775277
case ZEND_STRLEN:
52785278
return (t1 & MAY_BE_ANY) != MAY_BE_STRING;
52795279
case ZEND_COUNT:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
JIT ADD_ARRAY_ELEMENT: null key 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 test(null $k) {
17+
var_dump([0, $k => 1]);
18+
}
19+
20+
try {
21+
test(null);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Using null as an array offset is deprecated, use an empty string instead
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
JIT ARRAY_KEY_EXISTS: null key 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 test(null $k) {
17+
var_dump(array_key_exists($k, []));
18+
}
19+
20+
try {
21+
test(null);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Using null as the key parameter for array_key_exists() is deprecated, use an empty string instead
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
JIT ASSIGN_DIM: null key 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 test(null $k) {
17+
$a = [];
18+
var_dump($a[$k] = 1);
19+
}
20+
21+
try {
22+
test(null);
23+
} catch (Exception $e) {
24+
echo $e::class, ": ", $e->getMessage(), "\n";
25+
}
26+
?>
27+
--EXPECT--
28+
Exception: Using null as an array offset is deprecated, use an empty string instead
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
JIT FETCH_DIM_IS: null key 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 test(null $k) {
17+
$a = [];
18+
var_dump($a[$k] ?? 0);
19+
}
20+
21+
try {
22+
test(null);
23+
} catch (Exception $e) {
24+
echo $e::class, ": ", $e->getMessage(), "\n";
25+
}
26+
?>
27+
--EXPECT--
28+
Exception: Using null as an array offset is deprecated, use an empty string instead
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
JIT FETCH_DIM_W: null key 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+
class C {
17+
function __destruct() {
18+
echo "destruct\n";
19+
}
20+
}
21+
22+
function g($x, &$y) {}
23+
24+
function test(null $k) {
25+
$c = new C;
26+
g($c, $a[$k]);
27+
}
28+
29+
try {
30+
test(null);
31+
} catch (Exception $e) {
32+
echo $e::class, ": ", $e->getMessage(), "\n";
33+
}
34+
?>
35+
--EXPECT--
36+
destruct
37+
Exception: Using null as an array offset is deprecated, use an empty string instead

‎ext/opcache/tests/jit/gh25269.phpt‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
GH-25269 (JIT: misaligned zval passed to ZEND_ADD_ARRAY_ELEMENT handler)
3+
--CREDITS--
4+
YuanchengJiang
5+
--EXTENSIONS--
6+
opcache
7+
--INI--
8+
opcache.enable=1
9+
opcache.enable_cli=1
10+
opcache.jit_buffer_size=64M
11+
opcache.jit=function
12+
--FILE--
13+
<?php
14+
set_error_handler(function($errno, $errstr) {
15+
throw new ErrorException($errstr);
16+
});
17+
function test() {
18+
return [null => 1, "a" => 2, "b" => 3];
19+
}
20+
try {
21+
test();
22+
} catch (ErrorException $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Using null as an array offset is deprecated, use an empty string instead
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
JIT ISSET_ISEMPTY_DIM_OBJ: null key 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 test(null $k) {
17+
$a = [];
18+
var_dump(isset($a[$k]));
19+
}
20+
21+
try {
22+
test(null);
23+
} catch (Exception $e) {
24+
echo $e::class, ": ", $e->getMessage(), "\n";
25+
}
26+
?>
27+
--EXPECT--
28+
Exception: Using null as an array offset is deprecated, use an empty string instead

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
DCE must not remove ASSIGN_DIM with a null key
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(null $k) {
17+
$a[$k] = 1;
18+
}
19+
20+
try {
21+
test(null);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Using null as an array offset is deprecated, use an empty string instead

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
DCE must not remove INIT_ARRAY with a null key
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(null $k) {
17+
$a = [$k => 1];
18+
}
19+
20+
try {
21+
test(null);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Using null as an array offset is deprecated, use an empty string instead

0 commit comments

Comments
 (0)