Skip to content

Commit fe51365

Browse files
committed
Fix phpGH-16499: [JIT] Undefined to null coercion issues for return
1 parent d3b0efe commit fe51365

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Zend/Optimizer/zend_inference.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,6 +3895,9 @@ static zend_always_inline zend_result _zend_update_type_info(
38953895
} else {
38963896
zend_arg_info *ret_info = op_array->arg_info - 1;
38973897
tmp = zend_fetch_arg_info_type(script, ret_info, &ce);
3898+
if ((tmp & MAY_BE_NULL) && opline->op1_type == IS_CV) {
3899+
tmp |= MAY_BE_UNDEF;
3900+
}
38983901
tmp |= (t1 & MAY_BE_INDIRECT);
38993902

39003903
// TODO: We could model more precisely how illegal types are converted.

ext/opcache/jit/zend_jit_helpers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,12 @@ static bool ZEND_FASTCALL zend_jit_verify_arg_slow(zval *arg, zend_arg_info *arg
18281828

18291829
static void ZEND_FASTCALL zend_jit_verify_return_slow(zval *arg, const zend_op_array *op_array, zend_arg_info *arg_info, void **cache_slot)
18301830
{
1831+
if (Z_TYPE_P(arg) == IS_NULL) {
1832+
ZEND_ASSERT(ZEND_TYPE_IS_SET(arg_info->type));
1833+
if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(arg_info->type, IS_NULL))) {
1834+
return;
1835+
}
1836+
}
18311837
if (UNEXPECTED(!zend_check_user_type_slow(
18321838
&arg_info->type, arg, /* ref */ NULL, cache_slot, /* is_return_type */ true))) {
18331839
zend_verify_return_error((zend_function*)op_array, arg);

ext/opcache/tests/jit/gh16499.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
GH-16499 (Undefined to null coercion issues for return)
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.jit_buffer_size=64M
7+
--FILE--
8+
<?php
9+
function test($cond): ?int {
10+
if ($cond) {
11+
$i = 'foo';
12+
}
13+
return $i;
14+
}
15+
16+
var_dump(test(false));
17+
var_dump(test(false));
18+
?>
19+
--EXPECTF--
20+
Warning: Undefined variable $i in %sgh16499.php on line 6
21+
22+
Warning: Undefined variable $i in %sgh16499.php on line 6
23+
NULL
24+
25+
Warning: Undefined variable $i in %sgh16499.php on line 6
26+
27+
Warning: Undefined variable $i in %sgh16499.php on line 6
28+
NULL

0 commit comments

Comments
 (0)