Skip to content

Fix inheritance of class constants if mutable data used #7658

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 2 commits 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
35 changes: 35 additions & 0 deletions Zend/tests/class_constant_inheritance_mutable_data.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Class constant inheritance with mutable data
--FILE--
<?php

// This would previously leak under opcache.
class A {
const X = 'X' . self::Y;
const Y = 'Y';
}
interface I {
const X2 = 'X2' . self::Y2;
const Y2 = 'Y2';
}
eval('class B extends A implements I {}');
var_dump(new B);
var_dump(B::X, B::X2);

// This should only produce one warning, not two.
class X {
const C = 1 % 1.5;
}
class Y extends X {
}
var_dump(X::C, Y::C);
?>
--EXPECTF--
object(B)#1 (0) {
}
string(2) "XY"
string(4) "X2Y2"

Deprecated: Implicit conversion from float 1.5 to int loses precision in %s on line %d
int(0)
int(0)
23 changes: 19 additions & 4 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1323,9 +1323,14 @@ ZEND_API HashTable *zend_separate_class_constants_table(zend_class_entry *class_

ZEND_HASH_FOREACH_STR_KEY_PTR(&class_type->constants_table, key, c) {
if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
new_c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
memcpy(new_c, c, sizeof(zend_class_constant));
c = new_c;
if (c->ce == class_type) {
new_c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
memcpy(new_c, c, sizeof(zend_class_constant));
c = new_c;
} else {
c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), key);
ZEND_ASSERT(c);
}
}
Z_TRY_ADDREF(c->value);
_zend_hash_append_ptr(constants_table, key, c);
Expand Down Expand Up @@ -1409,8 +1414,18 @@ ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type) /
} else {
constants_table = &class_type->constants_table;
}
ZEND_HASH_FOREACH_PTR(constants_table, c) {

zend_string *name;
ZEND_HASH_FOREACH_STR_KEY_VAL(constants_table, name, val) {
c = Z_PTR_P(val);
if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
if (c->ce != class_type) {
Z_PTR_P(val) = c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), name);
if (Z_TYPE(c->value) != IS_CONSTANT_AST) {
continue;
}
}

val = &c->value;
if (UNEXPECTED(zval_update_constant_ex(val, c->ce) != SUCCESS)) {
return FAILURE;
Expand Down