Skip to content

Commit a225581

Browse files
committed
Fix GH-10747: Private and protected properties in serialized Date* objects throw
1 parent 4808fb6 commit a225581

File tree

8 files changed

+39
-6
lines changed

8 files changed

+39
-6
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ PHP NEWS
1010
. Re-add some CTE functions that were removed from being CTE by a mistake.
1111
(mvorisek)
1212

13+
- Date:
14+
. Fixed bug GH-10747 (Private and protected properties in serialized Date*
15+
objects throw). (Derick)
16+
1317
- FTP:
1418
. Propagate success status of ftp_close(). (nielsdos)
1519

ext/date/php_date.c

+34-4
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,36 @@ PHPAPI timelib_tzinfo *get_timezone_info(void)
565565
}
566566
return tzi;
567567
}
568+
569+
static void update_property(zend_object *object, zend_string *key, zval *prop_val)
570+
{
571+
if (ZSTR_VAL(key)[0] == '\0') { // not public
572+
const char *class_name, *prop_name;
573+
size_t prop_name_len;
574+
575+
if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
576+
if (class_name[0] != '*') { // private
577+
zend_string *cname;
578+
zend_class_entry *ce;
579+
580+
cname = zend_string_init(class_name, strlen(class_name), 0);
581+
ce = zend_lookup_class(cname);
582+
583+
if (ce) {
584+
zend_update_property(ce, object, prop_name, prop_name_len, prop_val);
585+
}
586+
587+
zend_string_release_ex(cname, 0);
588+
} else { // protected
589+
zend_update_property(object->ce, object, prop_name, prop_name_len, prop_val);
590+
}
591+
}
592+
return;
593+
}
594+
595+
// public
596+
zend_update_property(object->ce, object, ZSTR_VAL(key), ZSTR_LEN(key), prop_val);
597+
}
568598
/* }}} */
569599

570600

@@ -2794,7 +2824,7 @@ static void restore_custom_datetime_properties(zval *object, HashTable *myht)
27942824
if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_time_is_internal_property(prop_name)) {
27952825
continue;
27962826
}
2797-
add_property_zval_ex(object, ZSTR_VAL(prop_name), ZSTR_LEN(prop_name), prop_val);
2827+
update_property(Z_OBJ_P(object), prop_name, prop_val);
27982828
} ZEND_HASH_FOREACH_END();
27992829
}
28002830

@@ -3828,7 +3858,7 @@ static void restore_custom_datetimezone_properties(zval *object, HashTable *myht
38283858
if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_timezone_is_internal_property(prop_name)) {
38293859
continue;
38303860
}
3831-
add_property_zval_ex(object, ZSTR_VAL(prop_name), ZSTR_LEN(prop_name), prop_val);
3861+
update_property(Z_OBJ_P(object), prop_name, prop_val);
38323862
} ZEND_HASH_FOREACH_END();
38333863
}
38343864

@@ -4456,7 +4486,7 @@ static void restore_custom_dateinterval_properties(zval *object, HashTable *myht
44564486
if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_interval_is_internal_property(prop_name)) {
44574487
continue;
44584488
}
4459-
add_property_zval_ex(object, ZSTR_VAL(prop_name), ZSTR_LEN(prop_name), prop_val);
4489+
update_property(Z_OBJ_P(object), prop_name, prop_val);
44604490
} ZEND_HASH_FOREACH_END();
44614491
}
44624492

@@ -5418,7 +5448,7 @@ static void restore_custom_dateperiod_properties(zval *object, HashTable *myht)
54185448
if (!prop_name || (Z_TYPE_P(prop_val) == IS_REFERENCE) || date_period_is_internal_property(prop_name)) {
54195449
continue;
54205450
}
5421-
add_property_zval_ex(object, ZSTR_VAL(prop_name), ZSTR_LEN(prop_name), prop_val);
5451+
update_property(Z_OBJ_P(object), prop_name, prop_val);
54225452
} ZEND_HASH_FOREACH_END();
54235453
}
54245454

ext/date/tests/gh10152.phpt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
GH-10152: Custom properties of DateTimeImmutable child classes are not serialized
2+
Bug GH-10152 (Custom properties of DateTimeImmutable child classes are not serialized)
33
--FILE--
44
<?php
55

@@ -16,7 +16,6 @@ class MyDateTimeImmutable extends DateTimeImmutable {
1616
$datetime = new MyDateTimeImmutable('2022-12-22T11:26:00Z', myProperty: true);
1717
$serialized = serialize($datetime);
1818
$unserialized = unserialize($serialized);
19-
2019
var_dump($unserialized->myProperty);
2120
?>
2221
--EXPECT--

ext/date/tests/gh10747-1.phpt

1.07 KB
Binary file not shown.

ext/date/tests/gh10747-2.phpt

1.02 KB
Binary file not shown.

ext/date/tests/gh10747-3.phpt

1.38 KB
Binary file not shown.

ext/date/tests/gh10747-4.phpt

3.12 KB
Binary file not shown.

ext/date/tests/gh10747-error.phpt

1.03 KB
Binary file not shown.

0 commit comments

Comments
 (0)