diff --git a/NEWS b/NEWS index e9058420893..86b978760be 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,10 @@ PHP NEWS . Fixed bug GH-11433 (Unable to set CURLOPT_ACCEPT_ENCODING to NULL). (nielsdos) +- Date: + . Fixed bug GH-11455 (Segmentation fault with custom object date properties). + (nielsdos) + - DOM: . Fixed bugs GH-11288 and GH-11289 and GH-11290 and GH-9142 (DOMExceptions and segfaults with replaceWith). (nielsdos) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 2c622f66904..91420a94bd8 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 00000000000..cb6784f0b5a --- /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" +}