From 626d659b92e702f21747e7d677e48569fc735916 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Wed, 6 Jul 2022 10:57:33 +0200 Subject: [PATCH] Fix phpGH-8841: error on return type check of func declaration isn't removing the function from function_table --- Zend/zend_compile.c | 11 ++++- sapi/cli/tests/gh8841_1.phpt | 89 ++++++++++++++++++++++++++++++++++++ sapi/cli/tests/gh8841_2.phpt | 75 ++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 sapi/cli/tests/gh8841_1.phpt create mode 100644 sapi/cli/tests/gh8841_2.phpt diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index cf43dd852a7c..923661dd6e26 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -6992,7 +6992,16 @@ void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /* } else if (uses_ast) { zend_compile_closure_uses(uses_ast); } - zend_compile_stmt(stmt_ast); + + zend_try { + zend_compile_stmt(stmt_ast); + } zend_catch { + if (!is_method && toplevel) { + zend_hash_del(CG(function_table), CG(active_op_array)->function_name); + } + + zend_bailout(); + } zend_end_try(); if (is_method) { CG(zend_lineno) = decl->start_lineno; diff --git a/sapi/cli/tests/gh8841_1.phpt b/sapi/cli/tests/gh8841_1.phpt new file mode 100644 index 000000000000..f82af358458c --- /dev/null +++ b/sapi/cli/tests/gh8841_1.phpt @@ -0,0 +1,89 @@ +--TEST-- +GH-8841: Fix invalid return type compilation doesn't register function (without libedit) +--SKIPIF-- + +--FILE-- + $code) { + echo "\n--------------\nSnippet no. $key:\n--------------\n"; + $code = escapeshellarg($code); + echo `echo $code | "$php" -a`, "\n"; +} + +echo "\nDone\n"; +?> +--EXPECT-- +-------------- +Snippet no. 1: +-------------- +Interactive shell + +php > function f($x): void { return $x; } + +Fatal error: A void function must not return a value in php shell code on line 1 +php > f(1); + +Warning: Uncaught Error: Call to undefined function f() in php shell code:1 +Stack trace: +#0 {main} + thrown in php shell code on line 1 +php > + +-------------- +Snippet no. 2: +-------------- +Interactive shell + +php > function f($x): void { return $x; } + +Fatal error: A void function must not return a value in php shell code on line 1 +php > function f($x): int { return $x; } +php > echo f(1); +1 +php > + +-------------- +Snippet no. 3: +-------------- +Interactive shell + +php > function foo() { return $x[]; } + +Fatal error: Cannot use [] for reading in php shell code on line 1 +php > foo(); + +Warning: Uncaught Error: Call to undefined function foo() in php shell code:1 +Stack trace: +#0 {main} + thrown in php shell code on line 1 +php > + +Done diff --git a/sapi/cli/tests/gh8841_2.phpt b/sapi/cli/tests/gh8841_2.phpt new file mode 100644 index 000000000000..338b1c1d4728 --- /dev/null +++ b/sapi/cli/tests/gh8841_2.phpt @@ -0,0 +1,75 @@ +--TEST-- +GH-8841: Fix invalid return type compilation doesn't register function (with libedit) +--SKIPIF-- + +--FILE-- + $code) { + echo "\n--------------\nSnippet no. $key:\n--------------\n"; + $php = getenv('TEST_PHP_EXECUTABLE'); + $ini = getenv('TEST_PHP_EXTRA_ARGS'); + $descriptorspec = [['pipe', 'r'], STDOUT, STDERR]; + $proc = proc_open("$php $ini -a", $descriptorspec, $pipes); + fwrite($pipes[0], $code); + fclose($pipes[0]); + proc_close($proc); +} +?> +--EXPECT-- +-------------- +Snippet no. 1: +-------------- +Interactive shell + + +Fatal error: A void function must not return a value in php shell code on line 1 + +Warning: Uncaught Error: Call to undefined function f() in php shell code:1 +Stack trace: +#0 {main} + thrown in php shell code on line 1 + +-------------- +Snippet no. 2: +-------------- +Interactive shell + + +Fatal error: A void function must not return a value in php shell code on line 1 +1 + +-------------- +Snippet no. 3: +-------------- +Interactive shell + + +Fatal error: Cannot use [] for reading in php shell code on line 1 + +Warning: Uncaught Error: Call to undefined function foo() in php shell code:1 +Stack trace: +#0 {main} + thrown in php shell code on line 1