Fixed bug #66252 (Problems in AST evaluation invalidating valid parent:: reference. Constant expessions have to be evaluated in context of defining class).

This commit is contained in:
Dmitry Stogov 2013-12-10 14:19:17 +04:00
parent 5ad1117419
commit 5a87b7ff39
5 changed files with 67 additions and 53 deletions

14
Zend/tests/bug66252.phpt Normal file
View File

@ -0,0 +1,14 @@
--TEST--
Bug #66252 (Problems in AST evaluation invalidating valid parent:: reference)
--FILE--
<?php
class A {
const HW = "this is A";
}
class B extends A {
const BHW = parent::HW . " extended by B";
}
const C = B::BHW;
echo C, "\n";
--EXPECT--
this is A extended by B

View File

@ -81,143 +81,143 @@ ZEND_API int zend_ast_is_ct_constant(zend_ast *ast)
} }
} }
ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast TSRMLS_DC) ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope TSRMLS_DC)
{ {
zval op1, op2; zval op1, op2;
switch (ast->kind) { switch (ast->kind) {
case ZEND_ADD: case ZEND_ADD:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
add_function(result, &op1, &op2 TSRMLS_CC); add_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_SUB: case ZEND_SUB:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
sub_function(result, &op1, &op2 TSRMLS_CC); sub_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_MUL: case ZEND_MUL:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
mul_function(result, &op1, &op2 TSRMLS_CC); mul_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_DIV: case ZEND_DIV:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
div_function(result, &op1, &op2 TSRMLS_CC); div_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_MOD: case ZEND_MOD:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
mod_function(result, &op1, &op2 TSRMLS_CC); mod_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_SL: case ZEND_SL:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
shift_left_function(result, &op1, &op2 TSRMLS_CC); shift_left_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_SR: case ZEND_SR:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
shift_right_function(result, &op1, &op2 TSRMLS_CC); shift_right_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_CONCAT: case ZEND_CONCAT:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
concat_function(result, &op1, &op2 TSRMLS_CC); concat_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_BW_OR: case ZEND_BW_OR:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
bitwise_or_function(result, &op1, &op2 TSRMLS_CC); bitwise_or_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_BW_AND: case ZEND_BW_AND:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
bitwise_and_function(result, &op1, &op2 TSRMLS_CC); bitwise_and_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_BW_XOR: case ZEND_BW_XOR:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
bitwise_xor_function(result, &op1, &op2 TSRMLS_CC); bitwise_xor_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_BW_NOT: case ZEND_BW_NOT:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
bitwise_not_function(result, &op1 TSRMLS_CC); bitwise_not_function(result, &op1 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
break; break;
case ZEND_BOOL_NOT: case ZEND_BOOL_NOT:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
boolean_not_function(result, &op1 TSRMLS_CC); boolean_not_function(result, &op1 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
break; break;
case ZEND_BOOL_XOR: case ZEND_BOOL_XOR:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
boolean_xor_function(result, &op1, &op2 TSRMLS_CC); boolean_xor_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_IS_IDENTICAL: case ZEND_IS_IDENTICAL:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
is_identical_function(result, &op1, &op2 TSRMLS_CC); is_identical_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_IS_NOT_IDENTICAL: case ZEND_IS_NOT_IDENTICAL:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
is_not_identical_function(result, &op1, &op2 TSRMLS_CC); is_not_identical_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_IS_EQUAL: case ZEND_IS_EQUAL:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
is_equal_function(result, &op1, &op2 TSRMLS_CC); is_equal_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_IS_NOT_EQUAL: case ZEND_IS_NOT_EQUAL:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
is_not_equal_function(result, &op1, &op2 TSRMLS_CC); is_not_equal_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_IS_SMALLER: case ZEND_IS_SMALLER:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
is_smaller_function(result, &op1, &op2 TSRMLS_CC); is_smaller_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_IS_SMALLER_OR_EQUAL: case ZEND_IS_SMALLER_OR_EQUAL:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
is_smaller_or_equal_function(result, &op1, &op2 TSRMLS_CC); is_smaller_or_equal_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
zval_dtor(&op2); zval_dtor(&op2);
@ -226,13 +226,13 @@ ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast TSRMLS_DC)
*result = *ast->u.val; *result = *ast->u.val;
zval_copy_ctor(result); zval_copy_ctor(result);
if (IS_CONSTANT_TYPE(Z_TYPE_P(result))) { if (IS_CONSTANT_TYPE(Z_TYPE_P(result))) {
zval_update_constant(&result, (void *) 1 TSRMLS_CC); zval_update_constant_ex(&result, (void *) 1, scope TSRMLS_CC);
} }
break; break;
case ZEND_BOOL_AND: case ZEND_BOOL_AND:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
if (zend_is_true(&op1)) { if (zend_is_true(&op1)) {
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
ZVAL_BOOL(result, zend_is_true(&op2)); ZVAL_BOOL(result, zend_is_true(&op2));
zval_dtor(&op2); zval_dtor(&op2);
} else { } else {
@ -241,39 +241,39 @@ ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast TSRMLS_DC)
zval_dtor(&op1); zval_dtor(&op1);
break; break;
case ZEND_BOOL_OR: case ZEND_BOOL_OR:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
if (zend_is_true(&op1)) { if (zend_is_true(&op1)) {
ZVAL_BOOL(result, 1); ZVAL_BOOL(result, 1);
} else { } else {
zend_ast_evaluate(&op2, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC);
ZVAL_BOOL(result, zend_is_true(&op2)); ZVAL_BOOL(result, zend_is_true(&op2));
zval_dtor(&op2); zval_dtor(&op2);
} }
zval_dtor(&op1); zval_dtor(&op1);
break; break;
case ZEND_SELECT: case ZEND_SELECT:
zend_ast_evaluate(&op1, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC);
if (zend_is_true(&op1)) { if (zend_is_true(&op1)) {
if (!(&ast->u.child)[1]) { if (!(&ast->u.child)[1]) {
*result = op1; *result = op1;
} else { } else {
zend_ast_evaluate(result, (&ast->u.child)[1] TSRMLS_CC); zend_ast_evaluate(result, (&ast->u.child)[1], scope TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
} }
} else { } else {
zend_ast_evaluate(result, (&ast->u.child)[2] TSRMLS_CC); zend_ast_evaluate(result, (&ast->u.child)[2], scope TSRMLS_CC);
zval_dtor(&op1); zval_dtor(&op1);
} }
break; break;
case ZEND_UNARY_PLUS: case ZEND_UNARY_PLUS:
ZVAL_LONG(&op1, 0); ZVAL_LONG(&op1, 0);
zend_ast_evaluate(&op2, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[0], scope TSRMLS_CC);
add_function(result, &op1, &op2 TSRMLS_CC); add_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op2); zval_dtor(&op2);
break; break;
case ZEND_UNARY_MINUS: case ZEND_UNARY_MINUS:
ZVAL_LONG(&op1, 0); ZVAL_LONG(&op1, 0);
zend_ast_evaluate(&op2, (&ast->u.child)[0] TSRMLS_CC); zend_ast_evaluate(&op2, (&ast->u.child)[0], scope TSRMLS_CC);
sub_function(result, &op1, &op2 TSRMLS_CC); sub_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op2); zval_dtor(&op2);
break; break;

View File

@ -53,7 +53,7 @@ ZEND_API zend_ast *zend_ast_create_ternary(uint kind, zend_ast *op0, zend_ast *o
ZEND_API int zend_ast_is_ct_constant(zend_ast *ast); ZEND_API int zend_ast_is_ct_constant(zend_ast *ast);
ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast TSRMLS_DC); ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope TSRMLS_DC);
ZEND_API zend_ast *zend_ast_copy(zend_ast *ast); ZEND_API zend_ast *zend_ast_copy(zend_ast *ast);
ZEND_API void zend_ast_destroy(zend_ast *ast); ZEND_API void zend_ast_destroy(zend_ast *ast);

View File

@ -7341,7 +7341,7 @@ void zend_do_constant_expression(znode *result, zend_ast *ast TSRMLS_DC) /* {{{
result->u.constant = *ast->u.val; result->u.constant = *ast->u.val;
efree(ast); efree(ast);
} else if (zend_ast_is_ct_constant(ast)) { } else if (zend_ast_is_ct_constant(ast)) {
zend_ast_evaluate(&result->u.constant, ast TSRMLS_CC); zend_ast_evaluate(&result->u.constant, ast, NULL TSRMLS_CC);
zend_ast_destroy(ast); zend_ast_destroy(ast);
} else { } else {
Z_TYPE(result->u.constant) = IS_CONSTANT_AST; Z_TYPE(result->u.constant) = IS_CONSTANT_AST;

View File

@ -593,7 +593,7 @@ ZEND_API int zval_update_constant_ex(zval **pp, void *arg, zend_class_entry *sco
continue; continue;
} }
if (str_index[str_index_len - 2] == IS_CONSTANT_AST) { if (str_index[str_index_len - 2] == IS_CONSTANT_AST) {
zend_ast_evaluate(&const_value, *(zend_ast **)str_index TSRMLS_CC); zend_ast_evaluate(&const_value, *(zend_ast **)str_index, scope TSRMLS_CC);
zend_ast_destroy(*(zend_ast **)str_index); zend_ast_destroy(*(zend_ast **)str_index);
} else if (!zend_get_constant_ex(str_index, str_index_len - 3, &const_value, scope, str_index[str_index_len - 2] TSRMLS_CC)) { } else if (!zend_get_constant_ex(str_index, str_index_len - 3, &const_value, scope, str_index[str_index_len - 2] TSRMLS_CC)) {
char *actual; char *actual;
@ -667,7 +667,7 @@ ZEND_API int zval_update_constant_ex(zval **pp, void *arg, zend_class_entry *sco
SEPARATE_ZVAL_IF_NOT_REF(pp); SEPARATE_ZVAL_IF_NOT_REF(pp);
p = *pp; p = *pp;
zend_ast_evaluate(&const_value, Z_AST_P(p) TSRMLS_CC); zend_ast_evaluate(&const_value, Z_AST_P(p), scope TSRMLS_CC);
if (inline_change) { if (inline_change) {
zend_ast_destroy(Z_AST_P(p)); zend_ast_destroy(Z_AST_P(p));
} }