Skip to content

Commit 868930e

Browse files
committed
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.
1 parent 21a05b0 commit 868930e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Zend/tests/declare_006.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Use of non-literals in declare ticks values crashes compiler
3+
--FILE--
4+
<?php
5+
declare(ticks = UNKNOWN_CONST) {
6+
echo 'Done';
7+
}
8+
--EXPECTF--
9+
10+
Fatal error: declare(ticks) value must be a literal in %sdeclare_006.php on line 2

Zend/zend_compile.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4366,8 +4366,12 @@ void zend_compile_declare(zend_ast *ast) /* {{{ */
43664366
zend_ast *declare_ast = declares->child[i];
43674367
zend_ast *name_ast = declare_ast->child[0];
43684368
zend_ast *value_ast = declare_ast->child[1];
4369-
43704369
zend_string *name = zend_ast_get_str(name_ast);
4370+
4371+
if (value_ast->kind != ZEND_AST_ZVAL) {
4372+
zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
4373+
}
4374+
43714375
if (zend_string_equals_literal_ci(name, "ticks")) {
43724376
zval value_zv;
43734377
zend_const_expr_to_zval(&value_zv, value_ast);

0 commit comments

Comments
 (0)