Skip to content

Fix comp-time and constant evaluation of dynamic class constant fetch #10487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Zend/Optimizer/compact_literals.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,9 @@ void zend_optimizer_compact_literals(zend_op_array *op_array, zend_optimizer_ctx
}
break;
case ZEND_FETCH_CLASS_CONSTANT:
if (opline->op1_type == IS_CONST && opline->op2_type == IS_CONST) {
if (opline->op1_type == IS_CONST
&& opline->op2_type == IS_CONST
&& Z_TYPE(op_array->literals[opline->op2.constant]) == IS_STRING) {
// op1/op2 class_const
opline->extended_value = add_static_slot(&hash, op_array,
opline->op1.constant,
Expand Down
11 changes: 11 additions & 0 deletions Zend/tests/gh10486.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Assertion error when attempting comp-time eval of dynamic class constant fetch
--FILE--
<?php
y::{5}::y;
?>
--EXPECTF--
Fatal error: Uncaught Error: Class "y" not found in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
11 changes: 11 additions & 0 deletions Zend/tests/gh10486_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Assertion error when attempting constant eval of dynamic class constant fetch
--FILE--
<?php
const y = y::{y};
?>
--EXPECTF--
Fatal error: Uncaught Error: Undefined constant "y" in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
12 changes: 11 additions & 1 deletion Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,15 @@ ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate_ex(
case ZEND_AST_CLASS_CONST:
{
zend_string *class_name = zend_ast_get_str(ast->child[0]);
zend_string *const_name = zend_ast_get_str(ast->child[1]);
if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
return FAILURE;
}
if (UNEXPECTED(Z_TYPE(op2) != IS_STRING)) {
zend_invalid_class_constant_type_error(Z_TYPE(op2));
zval_ptr_dtor_nogc(&op2);
return FAILURE;
}
zend_string *const_name = Z_STR(op2);

zend_string *previous_filename;
zend_long previous_lineno;
Expand All @@ -821,9 +829,11 @@ ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate_ex(

if (UNEXPECTED(zv == NULL)) {
ZVAL_UNDEF(result);
zval_ptr_dtor_nogc(&op2);
return FAILURE;
}
ZVAL_COPY_OR_DUP(result, zv);
zval_ptr_dtor_nogc(&op2);
break;
}
case ZEND_AST_NEW:
Expand Down
5 changes: 5 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -10742,6 +10742,11 @@ static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
zend_ast *name_ast;
zend_string *resolved_name;

if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
|| Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
return;
}

zend_eval_const_expr(&ast->child[0]);
zend_eval_const_expr(&ast->child[1]);

Expand Down
5 changes: 5 additions & 0 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,11 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_indirect_modificati
ZSTR_VAL(info->ce->name), zend_get_unmangled_property_name(info->name));
}

ZEND_API ZEND_COLD void ZEND_FASTCALL zend_invalid_class_constant_type_error(zend_uchar type)
{
zend_type_error("Cannot use value of type %s as class constant name", zend_get_type_by_const(type));
}

static const zend_class_entry *resolve_single_class_type(zend_string *name, const zend_class_entry *self_ce) {
if (zend_string_equals_literal_ci(name, "self")) {
return self_ce;
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ ZEND_API ZEND_COLD void zend_wrong_string_offset_error(void);
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_modification_error(const zend_property_info *info);
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_indirect_modification_error(const zend_property_info *info);

ZEND_API ZEND_COLD void ZEND_FASTCALL zend_invalid_class_constant_type_error(zend_uchar type);

ZEND_API bool zend_verify_scalar_type_hint(uint32_t type_mask, zval *arg, bool strict, bool is_internal_arg);
ZEND_API ZEND_COLD void zend_verify_arg_error(
const zend_function *zf, const zend_arg_info *arg_info, uint32_t arg_num, zval *value);
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -5922,7 +5922,7 @@ ZEND_VM_HANDLER(181, ZEND_FETCH_CLASS_CONSTANT, VAR|CONST|UNUSED|CLASS_FETCH, CO

constant_zv = GET_OP2_ZVAL_PTR_DEREF(BP_VAR_R);
if (UNEXPECTED(Z_TYPE_P(constant_zv) != IS_STRING)) {
zend_type_error("Cannot use value of type %s as class constant name", zend_get_type_by_const(Z_TYPE_P(constant_zv)));
zend_invalid_class_constant_type_error(Z_TYPE_P(constant_zv));
ZVAL_UNDEF(EX_VAR(opline->result.var));
FREE_OP2();
HANDLE_EXCEPTION();
Expand Down
12 changes: 6 additions & 6 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.