Skip to content

Commit 74d0684

Browse files
Fix GH-8841: error on return type check of func declaration isn't removing the function from function_table
1 parent 8fce70a commit 74d0684

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

Zend/zend_compile.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4794,8 +4794,13 @@ void zend_compile_return(zend_ast *ast) /* {{{ */
47944794

47954795
/* Generator return types are handled separately */
47964796
if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
4797-
zend_emit_return_type_check(
4798-
expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
4797+
zend_try {
4798+
zend_emit_return_type_check(
4799+
expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
4800+
} zend_catch {
4801+
zend_hash_del(CG(function_table), CG(active_op_array)->function_name);
4802+
zend_bailout();
4803+
} zend_end_try();
47994804
}
48004805

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

sapi/cli/tests/gh8841.phpt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
GH-8841: Fix invalid return type compilation doesn't register function
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
?>
7+
--FILE--
8+
<?php
9+
$php = getenv('TEST_PHP_EXECUTABLE');
10+
11+
// disallow console escape sequences that may break the output
12+
putenv('TERM=VT100');
13+
14+
$codes = array();
15+
16+
$codes[1] = <<<EOT
17+
function f(\$x): void { return \$x; }
18+
f(1);
19+
EOT;
20+
21+
$codes[2] = <<<EOT
22+
function f(\$x): void { return \$x; }
23+
function f(\$x): int { return \$x; }
24+
echo f(1);
25+
EOT;
26+
27+
foreach ($codes as $key => $code) {
28+
echo "\n--------------\nSnippet no. $key:\n--------------\n";
29+
$code = escapeshellarg($code);
30+
echo `echo $code | "$php" -a`, "\n";
31+
}
32+
33+
echo "\nDone\n";
34+
?>
35+
--EXPECT--
36+
--------------
37+
Snippet no. 1:
38+
--------------
39+
Interactive shell
40+
41+
php > function f($x): void { return $x; }
42+
43+
Fatal error: A void function must not return a value in php shell code on line 1
44+
php > f(1);
45+
46+
Warning: Uncaught Error: Call to undefined function f() in php shell code:1
47+
Stack trace:
48+
#0 {main}
49+
thrown in php shell code on line 1
50+
php >
51+
52+
--------------
53+
Snippet no. 2:
54+
--------------
55+
Interactive shell
56+
57+
php > function f($x): void { return $x; }
58+
59+
Fatal error: A void function must not return a value in php shell code on line 1
60+
php > function f($x): int { return $x; }
61+
php > echo f(1);
62+
1
63+
php >
64+
65+
Done

0 commit comments

Comments
 (0)