Skip to content

Commit f320a47

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 f320a47

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 \$a[]; }
32+
foo();
33+
EOT;
34+
35+
$codes[4] = <<<EOT
36+
function bar(): never { return true; }
37+
bar();
38+
EOT;
39+
40+
foreach ($codes as $key => $code) {
41+
echo "\n--------------\nSnippet no. $key:\n--------------\n";
42+
$code = escapeshellarg($code);
43+
echo `echo $code | "$php" -a`, "\n";
44+
}
45+
46+
echo "\nDone\n";
47+
?>
48+
--EXPECT--
49+
--------------
50+
Snippet no. 1:
51+
--------------
52+
Interactive shell
53+
54+
php > function f($x): void { return $x; }
55+
56+
Fatal error: A void function must not return a value in php shell code on line 1
57+
php > f(1);
58+
59+
Warning: Uncaught Error: Call to undefined function f() in php shell code:1
60+
Stack trace:
61+
#0 {main}
62+
thrown in php shell code on line 1
63+
php >
64+
65+
--------------
66+
Snippet no. 2:
67+
--------------
68+
Interactive shell
69+
70+
php > function f($x): void { return $x; }
71+
72+
Fatal error: A void function must not return a value in php shell code on line 1
73+
php > function f($x): int { return $x; }
74+
php > echo f(1);
75+
1
76+
php >
77+
78+
--------------
79+
Snippet no. 3:
80+
--------------
81+
Interactive shell
82+
83+
php > function foo() { return $a[]; }
84+
85+
Fatal error: Cannot use [] for reading in php shell code on line 1
86+
php > foo();
87+
88+
Warning: Uncaught Error: Call to undefined function foo() in php shell code:1
89+
Stack trace:
90+
#0 {main}
91+
thrown in php shell code on line 1
92+
php >
93+
94+
--------------
95+
Snippet no. 4:
96+
--------------
97+
Interactive shell
98+
99+
php > function bar(): never { return true; }
100+
101+
Fatal error: A never-returning function must not return in php shell code on line 1
102+
php > bar();
103+
104+
Warning: Uncaught Error: Call to undefined function bar() in php shell code:1
105+
Stack trace:
106+
#0 {main}
107+
thrown in php shell code on line 1
108+
php >
109+
110+
Done

0 commit comments

Comments
 (0)