Skip to content

Commit b3f4863

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix circumvented type check with return by ref + finally
2 parents 5d40592 + d0fad34 commit b3f4863

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ PHP NEWS
2222
. Fixed bug GH-19326 (Calling Generator::throw() on a running generator with
2323
a non-Generator delegate crashes). (Arnaud)
2424
. Fixed bug GH-19280 (Stale array iterator position on rehashing). (ilutov)
25+
. Fixed bug GH-18736 (Circumvented type check with return by ref + finally).
26+
(ilutov)
2527

2628
- FTP:
2729
. Fix theoretical issues with hrtime() not being available. (nielsdos)

Zend/tests/gh18736.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
GH-18736: Circumvented type check with return by ref + finally
3+
--FILE--
4+
<?php
5+
6+
function &test(): int {
7+
$x = 0;
8+
try {
9+
return $x;
10+
} finally {
11+
$x = 'test';
12+
}
13+
}
14+
15+
try {
16+
$x = &test();
17+
var_dump($x);
18+
} catch (Error $e) {
19+
echo $e->getMessage(), "\n";
20+
}
21+
22+
?>
23+
--EXPECT--
24+
test(): Return value must be of type int, string returned

Zend/zend_compile.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5725,8 +5725,20 @@ static void zend_compile_return(zend_ast *ast) /* {{{ */
57255725
expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
57265726
}
57275727

5728+
uint32_t opnum_before_finally = get_next_op_number();
5729+
57285730
zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
57295731

5732+
/* Content of reference might have changed in finally, repeat type check. */
5733+
if (by_ref
5734+
/* Check if any opcodes were emitted since the last return type check. */
5735+
&& opnum_before_finally != get_next_op_number()
5736+
&& !is_generator
5737+
&& (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5738+
zend_emit_return_type_check(
5739+
expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
5740+
}
5741+
57305742
opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
57315743
&expr_node, NULL);
57325744

0 commit comments

Comments
 (0)