Fix GH-11455: Segmentation fault with custom object date properties

Closes GH-11473.
This commit is contained in:
Niels Dossche 2023-06-18 13:33:15 +02:00 committed by nielsdos
parent de0223113a
commit 93becab506
3 changed files with 46 additions and 1 deletions

4
NEWS
View file

@ -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)

View file

@ -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();
}

View file

@ -0,0 +1,39 @@
--TEST--
Bug GH-11455 (PHP 8.2 Segmentation fault on nesbot/carbon)
--FILE--
<?php
class MyDateTimeImmutable extends DateTimeImmutable {
public function __construct(
string $datetime = "now",
?DateTimeZone $timezone = null,
public ?stdClass $myProperty = null,
) {
parent::__construct($datetime, $timezone);
}
}
$datetime = new MyDateTimeImmutable('2022-12-22T11:26:00Z', myProperty: new stdClass);
$datetime->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"
}