Skip to content

Commit

Permalink
Fix GH-11455: Segmentation fault with custom object date properties
Browse files Browse the repository at this point in the history
Closes GH-11473.
  • Loading branch information
nielsdos committed Jun 19, 2023
1 parent de02231 commit 93becab
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion ext/date/php_date.c
Expand Up @@ -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();
}

Expand Down
39 changes: 39 additions & 0 deletions ext/date/tests/gh11455.phpt
@@ -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"
}

0 comments on commit 93becab

Please sign in to comment.