Skip to content

Commit bed8459

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 bed8459

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

Zend/zend_compile.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6992,7 +6992,13 @@ void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /*
69926992
} else if (uses_ast) {
69936993
zend_compile_closure_uses(uses_ast);
69946994
}
6995-
zend_compile_stmt(stmt_ast);
6995+
6996+
zend_try {
6997+
zend_compile_stmt(stmt_ast);
6998+
} zend_catch {
6999+
zend_hash_del(CG(function_table), CG(active_op_array)->function_name);
7000+
zend_bailout();
7001+
} zend_end_try();
69967002

69977003
if (is_method) {
69987004
CG(zend_lineno) = decl->start_lineno;

sapi/cli/tests/gh8841.phpt

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
$codes[3] = <<<EOT
31+
function foo() { return \$x[]; }
32+
foo();
33+
EOT;
34+
35+
echo "\nDone\n";
36+
?>
37+
--EXPECT--
38+
--------------
39+
Snippet no. 1:
40+
--------------
41+
Interactive shell
42+
43+
php > function f($x): void { return $x; }
44+
45+
Fatal error: A void function must not return a value in php shell code on line 1
46+
php > f(1);
47+
48+
Warning: Uncaught Error: Call to undefined function f() in php shell code:1
49+
Stack trace:
50+
#0 {main}
51+
thrown in php shell code on line 1
52+
php >
53+
54+
--------------
55+
Snippet no. 2:
56+
--------------
57+
Interactive shell
58+
59+
php > function f($x): void { return $x; }
60+
61+
Fatal error: A void function must not return a value in php shell code on line 1
62+
php > function f($x): int { return $x; }
63+
php > echo f(1);
64+
1
65+
php >
66+
67+
--------------
68+
Snippet no. 3:
69+
--------------
70+
Interactive shell
71+
72+
php > function foo() { return $x[]; }
73+
74+
Fatal error: Cannot use [] for reading in php shell code on line 1
75+
php > foo();
76+
77+
Warning: Uncaught Error: Call to undefined function foo() in php shell code:1
78+
Stack trace:
79+
#0 {main}
80+
thrown in php shell code on line 1
81+
php >
82+
83+
Done

0 commit comments

Comments
 (0)