Skip to content

Commit 8c7512e

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 8c7512e

File tree

2 files changed

+75
-2
lines changed

2 files changed

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

0 commit comments

Comments
 (0)