Skip to content

Commit 0d12b3d

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Fix GH-10990: mail() throws TypeError after iterating over $additional_headers array by reference Fix GH-8841: php-cli core dump calling a badly formed function
2 parents d1fc88c + 79c5b32 commit 0d12b3d

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ PHP NEWS
44

55
- Core:
66
. Fix inconsistent float negation in constant expressions. (ilutov)
7+
. Fixed bug GH-8841 (php-cli core dump calling a badly formed function).
8+
(nielsdos)
79

810
- DOM:
911
. Fixed bug #80602 (Segfault when using DOMChildNode::before()).
@@ -16,6 +18,10 @@ PHP NEWS
1618
. Handle indirect zvals and use up-to-date properties in
1719
SplFixedArray::__serialize. (nielsdos)
1820

21+
- Standard:
22+
. Fixed bug GH-10990 (mail() throws TypeError after iterating over
23+
$additional_headers array by reference). (nielsdos)
24+
1925
- Streams:
2026
. Fixed bug GH-10406 (feof() behavior change for UNIX based socket
2127
resources). (Jakub Zelenka)

Zend/tests/gh8841.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-8841 (php-cli core dump calling a badly formed function)
3+
--FILE--
4+
<?php
5+
register_shutdown_function(function() {
6+
echo "Before calling g()\n";
7+
g(1);
8+
echo "After calling g()\n";
9+
});
10+
11+
register_shutdown_function(function() {
12+
echo "Before calling f()\n";
13+
f(1);
14+
echo "After calling f()\n";
15+
});
16+
17+
eval('function g($x): int { return $x; }');
18+
eval('function f($x): void { return $x; }');
19+
?>
20+
--EXPECTF--
21+
Fatal error: A void function must not return a value in %s on line %d
22+
Before calling g()
23+
After calling g()
24+
Before calling f()
25+
26+
Fatal error: Uncaught Error: Call to undefined function f() in %s:%d
27+
Stack trace:
28+
#0 [internal function]: {closure}()
29+
#1 {main}
30+
thrown in %s on line %d

Zend/zend_compile.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7302,11 +7302,7 @@ static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array,
73027302
}
73037303

73047304
zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
7305-
if (toplevel) {
7306-
if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
7307-
do_bind_function_error(lcname, op_array, 1);
7308-
}
7309-
} else {
7305+
if (!toplevel) {
73107306
uint32_t func_ref = zend_add_dynamic_func_def(op_array);
73117307
if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
73127308
opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
@@ -7331,7 +7327,7 @@ static void zend_compile_func_decl(znode *result, zend_ast *ast, bool toplevel)
73317327
zend_ast *stmt_ast = decl->child[2];
73327328
zend_ast *return_type_ast = decl->child[3];
73337329
bool is_method = decl->kind == ZEND_AST_METHOD;
7334-
zend_string *lcname = NULL;
7330+
zend_string *lcname;
73357331

73367332
zend_class_entry *orig_class_entry = CG(active_class_entry);
73377333
zend_op_array *orig_op_array = CG(active_op_array);
@@ -7435,6 +7431,11 @@ static void zend_compile_func_decl(znode *result, zend_ast *ast, bool toplevel)
74357431
CG(zend_lineno) = decl->start_lineno;
74367432
zend_check_magic_method_implementation(
74377433
CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
7434+
} else if (toplevel) {
7435+
/* Only register the function after a successful compile */
7436+
if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
7437+
do_bind_function_error(lcname, op_array, true);
7438+
}
74387439
}
74397440

74407441
/* put the implicit return on the really last line */

ext/standard/mail.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ static void php_mail_build_headers_elems(smart_str *s, zend_string *key, zval *v
136136
zend_type_error("Header \"%s\" must only contain numeric keys, \"%s\" found", ZSTR_VAL(key), ZSTR_VAL(tmp_key));
137137
break;
138138
}
139+
ZVAL_DEREF(tmp_val);
139140
if (Z_TYPE_P(tmp_val) != IS_STRING) {
140141
zend_type_error("Header \"%s\" must only contain values of type string, %s found", ZSTR_VAL(key), zend_zval_type_name(tmp_val));
141142
break;
@@ -157,6 +158,7 @@ PHPAPI zend_string *php_mail_build_headers(HashTable *headers)
157158
zend_type_error("Header name cannot be numeric, " ZEND_LONG_FMT " given", idx);
158159
break;
159160
}
161+
ZVAL_DEREF(val);
160162
/* https://tools.ietf.org/html/rfc2822#section-3.6 */
161163
if (zend_string_equals_literal_ci(key, "orig-date")) {
162164
PHP_MAIL_BUILD_HEADER_CHECK("orig-date", s, key, val);

ext/standard/tests/mail/gh10990.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-10990 (mail() throws TypeError after iterating over $additional_headers array by reference)
3+
--INI--
4+
sendmail_path=rubbish 2>/dev/null
5+
--SKIPIF--
6+
<?php
7+
if(substr(PHP_OS, 0, 3) == "WIN")
8+
die("skip Won't run on Windows");
9+
?>
10+
--FILE--
11+
<?php
12+
13+
$headers = ['From' => &$from];
14+
var_dump(mail('[email protected]', 'Test', 'Test', $headers));
15+
?>
16+
--EXPECT--
17+
bool(false)

0 commit comments

Comments
 (0)