Fix potential crash when setting invalid declare value

Using a non-literal expression in a declare value can cause the
compiler to crash trying to turn that AST node into a usable zval.

There was an existing test for such values using 'encoding',
but that didn't crash because it's handled by the lexer
rather than being compiled.

Trying to use a non-literal with ticks reproduces the crash.
This commit is contained in:
Sara Golemon 2017-02-22 13:56:38 -08:00
parent 21a05b0418
commit 868930e079
2 changed files with 15 additions and 1 deletions

View File

@ -0,0 +1,10 @@
--TEST--
Use of non-literals in declare ticks values crashes compiler
--FILE--
<?php
declare(ticks = UNKNOWN_CONST) {
echo 'Done';
}
--EXPECTF--
Fatal error: declare(ticks) value must be a literal in %sdeclare_006.php on line 2

View File

@ -4366,8 +4366,12 @@ void zend_compile_declare(zend_ast *ast) /* {{{ */
zend_ast *declare_ast = declares->child[i];
zend_ast *name_ast = declare_ast->child[0];
zend_ast *value_ast = declare_ast->child[1];
zend_string *name = zend_ast_get_str(name_ast);
if (value_ast->kind != ZEND_AST_ZVAL) {
zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
}
if (zend_string_equals_literal_ci(name, "ticks")) {
zval value_zv;
zend_const_expr_to_zval(&value_zv, value_ast);