Skip to content

Commit f9a58fc

Browse files
committed
Fix circumvented type check with return by ref + finally
Fixes GH-18736
1 parent 419f675 commit f9a58fc

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5703,6 +5703,15 @@ static void zend_compile_return(zend_ast *ast) /* {{{ */
57035703

57045704
zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
57055705

5706+
/* Content of reference might have changed in finally, repeat type check. */
5707+
if (by_ref
5708+
&& !zend_stack_is_empty(&CG(loop_var_stack))
5709+
&& !is_generator
5710+
&& (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5711+
zend_emit_return_type_check(
5712+
expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
5713+
}
5714+
57065715
opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
57075716
&expr_node, NULL);
57085717

0 commit comments

Comments
 (0)