Skip to content

Commit f9b7609

Browse files
committed
Fixed bug #80225
Namespaced and declares have a different interpretation of what "first statement" means.
1 parent 11c752a commit f9b7609

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.0.0rc2
44

5+
- Core:
6+
. Fixed bug #80225 (broken namespace usage in eval code). (Nikita)
7+
58
- Curl:
69
. Fixed bug #80121 (Null pointer deref if CurlHandle directly instantiated).
710
(Nikita)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Nop statement before namespace
3+
--FILE--
4+
<?php
5+
;
6+
namespace Foo;
7+
?>
8+
===DONE===
9+
--EXPECT--
10+
===DONE===

Zend/zend_compile.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5865,7 +5865,7 @@ zend_bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
58655865
/* }}} */
58665866

58675867
/* Check whether this is the first statement, not counting declares. */
5868-
static zend_result zend_is_first_statement(zend_ast *ast) /* {{{ */
5868+
static zend_result zend_is_first_statement(zend_ast *ast, zend_bool allow_nop) /* {{{ */
58695869
{
58705870
uint32_t i = 0;
58715871
zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
@@ -5874,8 +5874,9 @@ static zend_result zend_is_first_statement(zend_ast *ast) /* {{{ */
58745874
if (file_ast->child[i] == ast) {
58755875
return SUCCESS;
58765876
} else if (file_ast->child[i] == NULL) {
5877-
/* Empty statements count as statements. */
5878-
return FAILURE;
5877+
if (!allow_nop) {
5878+
return FAILURE;
5879+
}
58795880
} else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
58805881
return FAILURE;
58815882
}
@@ -5909,14 +5910,14 @@ void zend_compile_declare(zend_ast *ast) /* {{{ */
59095910
zval_ptr_dtor_nogc(&value_zv);
59105911
} else if (zend_string_equals_literal_ci(name, "encoding")) {
59115912

5912-
if (FAILURE == zend_is_first_statement(ast)) {
5913+
if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ 0)) {
59135914
zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
59145915
"the very first statement in the script");
59155916
}
59165917
} else if (zend_string_equals_literal_ci(name, "strict_types")) {
59175918
zval value_zv;
59185919

5919-
if (FAILURE == zend_is_first_statement(ast)) {
5920+
if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ 0)) {
59205921
zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
59215922
"the very first statement in the script");
59225923
}
@@ -7669,7 +7670,7 @@ void zend_compile_namespace(zend_ast *ast) /* {{{ */
76697670

76707671
zend_bool is_first_namespace = (!with_bracket && !FC(current_namespace))
76717672
|| (with_bracket && !FC(has_bracketed_namespaces));
7672-
if (is_first_namespace && FAILURE == zend_is_first_statement(ast)) {
7673+
if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ 1)) {
76737674
zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
76747675
"the very first statement or after any declare call in the script");
76757676
}

0 commit comments

Comments
 (0)