Skip to content

Commit 2763f14

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Fix constant update for shadowed private property
2 parents bc39abe + 58699ff commit 2763f14

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Constant updating for shadowed private property
3+
--FILE--
4+
<?php
5+
class Foo {
6+
private $prop = X;
7+
function test() {
8+
var_dump($this->prop);
9+
}
10+
}
11+
12+
class Bar extends Foo {
13+
protected $prop;
14+
}
15+
16+
define('X', 1);
17+
$bar = new Bar;
18+
$bar->test();
19+
20+
?>
21+
--EXPECT--
22+
int(1)

Zend/zend_API.c

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,14 +1344,34 @@ ZEND_API HashTable *zend_separate_class_constants_table(zend_class_entry *class_
13441344
return constants_table;
13451345
}
13461346

1347+
static zend_result update_property(zval *val, zend_property_info *prop_info) {
1348+
if (ZEND_TYPE_IS_SET(prop_info->type)) {
1349+
zval tmp;
1350+
1351+
ZVAL_COPY(&tmp, val);
1352+
if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
1353+
zval_ptr_dtor(&tmp);
1354+
return FAILURE;
1355+
}
1356+
/* property initializers must always be evaluated with strict types */;
1357+
if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
1358+
zval_ptr_dtor(&tmp);
1359+
return FAILURE;
1360+
}
1361+
zval_ptr_dtor(val);
1362+
ZVAL_COPY_VALUE(val, &tmp);
1363+
return SUCCESS;
1364+
}
1365+
return zval_update_constant_ex(val, prop_info->ce);
1366+
}
1367+
13471368
ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
13481369
{
13491370
zend_class_mutable_data *mutable_data = NULL;
13501371
zval *default_properties_table = NULL;
13511372
zval *static_members_table = NULL;
13521373
zend_class_constant *c;
13531374
zval *val;
1354-
zend_property_info *prop_info;
13551375
uint32_t ce_flags;
13561376

13571377
ce_flags = class_type->ce_flags;
@@ -1430,33 +1450,30 @@ ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type) /
14301450
}
14311451

14321452
if (ce_flags & (ZEND_ACC_HAS_AST_PROPERTIES|ZEND_ACC_HAS_AST_STATICS)) {
1433-
ZEND_HASH_FOREACH_PTR(&class_type->properties_info, prop_info) {
1434-
if (prop_info->flags & ZEND_ACC_STATIC) {
1435-
val = static_members_table + prop_info->offset;
1436-
} else {
1437-
val = (zval*)((char*)default_properties_table + prop_info->offset - OBJ_PROP_TO_OFFSET(0));
1453+
zend_property_info *prop_info;
1454+
1455+
/* Use the default properties table to also update initializers of private properties
1456+
* that have been shadowed in a child class. */
1457+
for (uint32_t i = 0; i < class_type->default_properties_count; i++) {
1458+
val = &default_properties_table[i];
1459+
prop_info = class_type->properties_info_table[i];
1460+
if (Z_TYPE_P(val) == IS_CONSTANT_AST
1461+
&& UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
1462+
return FAILURE;
14381463
}
1439-
if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
1440-
if (ZEND_TYPE_IS_SET(prop_info->type)) {
1441-
zval tmp;
1464+
}
14421465

1443-
ZVAL_COPY(&tmp, val);
1444-
if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
1445-
zval_ptr_dtor(&tmp);
1446-
return FAILURE;
1447-
}
1448-
/* property initializers must always be evaluated with strict types */;
1449-
if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
1450-
zval_ptr_dtor(&tmp);
1466+
if (class_type->default_static_members_count) {
1467+
ZEND_HASH_FOREACH_PTR(&class_type->properties_info, prop_info) {
1468+
if (prop_info->flags & ZEND_ACC_STATIC) {
1469+
val = static_members_table + prop_info->offset;
1470+
if (Z_TYPE_P(val) == IS_CONSTANT_AST
1471+
&& UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
14511472
return FAILURE;
14521473
}
1453-
zval_ptr_dtor(val);
1454-
ZVAL_COPY_VALUE(val, &tmp);
1455-
} else if (UNEXPECTED(zval_update_constant_ex(val, prop_info->ce) != SUCCESS)) {
1456-
return FAILURE;
14571474
}
1458-
}
1459-
} ZEND_HASH_FOREACH_END();
1475+
} ZEND_HASH_FOREACH_END();
1476+
}
14601477
}
14611478

14621479
ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;

0 commit comments

Comments
 (0)