diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 2c622f66904a0..91420a94bd8ab 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -2305,7 +2305,9 @@ static void add_common_properties(HashTable *myht, zend_object *zobj) common = zend_std_get_properties(zobj); ZEND_HASH_MAP_FOREACH_STR_KEY_VAL_IND(common, name, prop) { - zend_hash_add(myht, name, prop); + if (zend_hash_add(myht, name, prop) != NULL) { + Z_TRY_ADDREF_P(prop); + } } ZEND_HASH_FOREACH_END(); } diff --git a/ext/date/tests/gh11455.phpt b/ext/date/tests/gh11455.phpt new file mode 100644 index 0000000000000..cb6784f0b5a71 --- /dev/null +++ b/ext/date/tests/gh11455.phpt @@ -0,0 +1,39 @@ +--TEST-- +Bug GH-11455 (PHP 8.2 Segmentation fault on nesbot/carbon) +--FILE-- +myProperty->field = str_repeat("hello", 3); +$serialized = serialize($datetime); +var_dump($datetime->myProperty); +$unserialized = unserialize($serialized); +var_dump($unserialized); +?> +--EXPECT-- +object(stdClass)#2 (1) { + ["field"]=> + string(15) "hellohellohello" +} +object(MyDateTimeImmutable)#3 (4) { + ["myProperty"]=> + object(stdClass)#4 (1) { + ["field"]=> + string(15) "hellohellohello" + } + ["date"]=> + string(26) "2022-12-22 11:26:00.000000" + ["timezone_type"]=> + int(2) + ["timezone"]=> + string(1) "Z" +}