From: Anthony Ferrara Date: Fri, 20 Feb 2015 16:43:18 +0000 (-0500) Subject: Fix declare error behavior so that improper usages will actually error X-Git-Tag: PRE_PHP7_EREG_MYSQL_REMOVALS~99 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e3b020b945582a45465ae2ae145af9c250be9e54;p=php Fix declare error behavior so that improper usages will actually error --- diff --git a/Zend/tests/bug69092.2.phpt b/Zend/tests/bug69092.2.phpt new file mode 100644 index 0000000000..b3f4bff175 --- /dev/null +++ b/Zend/tests/bug69092.2.phpt @@ -0,0 +1,18 @@ +--TEST-- +Bug #69092-2 (Declare Encoding Compile Check Wrong) - multibyte off +--INI-- +zend.multibyte=0 +--FILE-- + +--EXPECTF-- +Warning: declare(encoding=...) ignored because Zend multibyte feature is turned off by settings in %s on line %d + +Fatal error: Encoding declaration pragma must be the very first statement in the script in %s on line %d \ No newline at end of file diff --git a/Zend/tests/bug69092.phpt b/Zend/tests/bug69092.phpt new file mode 100644 index 0000000000..1963d7be02 --- /dev/null +++ b/Zend/tests/bug69092.phpt @@ -0,0 +1,22 @@ +--TEST-- +Bug #69092 (Declare Encoding Compile Check Wrong) +--SKIPIF-- + +--INI-- +zend.multibyte=On +--FILE-- + +--EXPECTF-- +Fatal error: Encoding declaration pragma must be the very first statement in the script in %s on line %d \ No newline at end of file diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 2608cf150a..c5479af516 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -3806,14 +3806,24 @@ void zend_compile_declare(zend_ast *ast) /* {{{ */ } else if (zend_string_equals_literal_ci(name, "encoding")) { /* Encoding declaration was already handled during parsing. Here we * only check that it is the first statement in the file. */ - uint32_t num = CG(active_op_array)->last; - while (num > 0 && - (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT || - CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) { - --num; + zend_ast_list *file_ast = zend_ast_get_list(CG(ast)); + + size_t i = 0; + signed char valid = 0; + + /* Check to see if this declare is preceeded only by declare statements */ + while (valid == 0 && i < file_ast->children) { + if (file_ast->child[i] == ast) { + valid = 1; + } else if (file_ast->child[i] == NULL) { + valid = -1; + } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) { + /* declares can only be preceeded by other declares */ + valid = -1; + } + i++; } - - if (num > 0) { + if (valid != 1) { zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be " "the very first statement in the script"); } diff --git a/ext/mbstring/tests/zend_multibyte-10.phpt b/ext/mbstring/tests/zend_multibyte-10.phpt index 138b4899fe..ef8ce9d0bc 100644 --- a/ext/mbstring/tests/zend_multibyte-10.phpt +++ b/ext/mbstring/tests/zend_multibyte-10.phpt @@ -9,4 +9,4 @@ declare(encoding="ISO-8859-1"); echo "ok\n"; ?> --EXPECTF-- -ok +ok \ No newline at end of file diff --git a/ext/mbstring/tests/zend_multibyte-11.phpt b/ext/mbstring/tests/zend_multibyte-11.phpt index 6844d06370..0f9874b538 100644 --- a/ext/mbstring/tests/zend_multibyte-11.phpt +++ b/ext/mbstring/tests/zend_multibyte-11.phpt @@ -10,4 +10,4 @@ declare(encoding="ISO-8859-15") { } ?> --EXPECTF-- -ok +Fatal error: Encoding declaration pragma must be the very first statement in the script in %s on line %d \ No newline at end of file diff --git a/ext/mbstring/tests/zend_multibyte-15.phpt b/ext/mbstring/tests/zend_multibyte-15.phpt new file mode 100644 index 0000000000..6995444bbb --- /dev/null +++ b/ext/mbstring/tests/zend_multibyte-15.phpt @@ -0,0 +1,16 @@ +--TEST-- +zend multibyte (15) +--INI-- +zend.multibyte=1 +--FILE-- + +--EXPECTF-- +ok +ok \ No newline at end of file diff --git a/ext/mbstring/tests/zend_multibyte-16.phpt b/ext/mbstring/tests/zend_multibyte-16.phpt new file mode 100644 index 0000000000..4470e3a87f --- /dev/null +++ b/ext/mbstring/tests/zend_multibyte-16.phpt @@ -0,0 +1,14 @@ +--TEST-- +zend multibyte (16) +--INI-- +zend.multibyte=1 +--FILE-- + +--EXPECTF-- +ok +ok \ No newline at end of file