mirror of
https://github.com/php/php-src.git
synced 2025-01-24 04:33:39 +08:00
Fix declare error behavior so that improper usages will actually error
This commit is contained in:
parent
cfae254b6b
commit
e3b020b945
18
Zend/tests/bug69092.2.phpt
Normal file
18
Zend/tests/bug69092.2.phpt
Normal file
@ -0,0 +1,18 @@
|
||||
--TEST--
|
||||
Bug #69092-2 (Declare Encoding Compile Check Wrong) - multibyte off
|
||||
--INI--
|
||||
zend.multibyte=0
|
||||
--FILE--
|
||||
<?php
|
||||
echo "Hi";
|
||||
|
||||
function foo() {
|
||||
declare(encoding="UTF-8");
|
||||
}
|
||||
|
||||
echo "Bye"
|
||||
?>
|
||||
--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
|
22
Zend/tests/bug69092.phpt
Normal file
22
Zend/tests/bug69092.phpt
Normal file
@ -0,0 +1,22 @@
|
||||
--TEST--
|
||||
Bug #69092 (Declare Encoding Compile Check Wrong)
|
||||
--SKIPIF--
|
||||
<?php
|
||||
if (!extension_loaded("mbstring")) {
|
||||
die("skip Requires mbstring extension");
|
||||
}
|
||||
?>
|
||||
--INI--
|
||||
zend.multibyte=On
|
||||
--FILE--
|
||||
<?php
|
||||
echo "Hi";
|
||||
|
||||
function foo() {
|
||||
declare(encoding="utf-8");
|
||||
}
|
||||
|
||||
echo "Bye"
|
||||
?>
|
||||
--EXPECTF--
|
||||
Fatal error: Encoding declaration pragma must be the very first statement in the script in %s on line %d
|
@ -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;
|
||||
|
||||
if (num > 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 (valid != 1) {
|
||||
zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
|
||||
"the very first statement in the script");
|
||||
}
|
||||
|
@ -9,4 +9,4 @@ declare(encoding="ISO-8859-1");
|
||||
echo "ok\n";
|
||||
?>
|
||||
--EXPECTF--
|
||||
ok
|
||||
ok
|
@ -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
|
16
ext/mbstring/tests/zend_multibyte-15.phpt
Normal file
16
ext/mbstring/tests/zend_multibyte-15.phpt
Normal file
@ -0,0 +1,16 @@
|
||||
--TEST--
|
||||
zend multibyte (15)
|
||||
--INI--
|
||||
zend.multibyte=1
|
||||
--FILE--
|
||||
<?php
|
||||
declare(encoding="ISO-8859-15") {
|
||||
echo "ok\n";
|
||||
}
|
||||
declare(encoding="UTF-8") {
|
||||
echo "ok\n";
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
ok
|
||||
ok
|
14
ext/mbstring/tests/zend_multibyte-16.phpt
Normal file
14
ext/mbstring/tests/zend_multibyte-16.phpt
Normal file
@ -0,0 +1,14 @@
|
||||
--TEST--
|
||||
zend multibyte (16)
|
||||
--INI--
|
||||
zend.multibyte=1
|
||||
--FILE--
|
||||
<?php
|
||||
declare(encoding="ISO-8859-15") {
|
||||
echo "ok\n";
|
||||
}
|
||||
echo "ok\n";
|
||||
?>
|
||||
--EXPECTF--
|
||||
ok
|
||||
ok
|
Loading…
Reference in New Issue
Block a user