Skip to content

Commit 93becab

Browse files
committed
Fix phpGH-11455: Segmentation fault with custom object date properties
Closes phpGH-11473.
1 parent de02231 commit 93becab

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ PHP NEWS
1313
. Fixed bug GH-11433 (Unable to set CURLOPT_ACCEPT_ENCODING to NULL).
1414
(nielsdos)
1515

16+
- Date:
17+
. Fixed bug GH-11455 (Segmentation fault with custom object date properties).
18+
(nielsdos)
19+
1620
- DOM:
1721
. Fixed bugs GH-11288 and GH-11289 and GH-11290 and GH-9142 (DOMExceptions
1822
and segfaults with replaceWith). (nielsdos)

ext/date/php_date.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -2305,7 +2305,9 @@ static void add_common_properties(HashTable *myht, zend_object *zobj)
23052305
common = zend_std_get_properties(zobj);
23062306

23072307
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL_IND(common, name, prop) {
2308-
zend_hash_add(myht, name, prop);
2308+
if (zend_hash_add(myht, name, prop) != NULL) {
2309+
Z_TRY_ADDREF_P(prop);
2310+
}
23092311
} ZEND_HASH_FOREACH_END();
23102312
}
23112313

ext/date/tests/gh11455.phpt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Bug GH-11455 (PHP 8.2 Segmentation fault on nesbot/carbon)
3+
--FILE--
4+
<?php
5+
class MyDateTimeImmutable extends DateTimeImmutable {
6+
public function __construct(
7+
string $datetime = "now",
8+
?DateTimeZone $timezone = null,
9+
public ?stdClass $myProperty = null,
10+
) {
11+
parent::__construct($datetime, $timezone);
12+
}
13+
}
14+
15+
$datetime = new MyDateTimeImmutable('2022-12-22T11:26:00Z', myProperty: new stdClass);
16+
$datetime->myProperty->field = str_repeat("hello", 3);
17+
$serialized = serialize($datetime);
18+
var_dump($datetime->myProperty);
19+
$unserialized = unserialize($serialized);
20+
var_dump($unserialized);
21+
?>
22+
--EXPECT--
23+
object(stdClass)#2 (1) {
24+
["field"]=>
25+
string(15) "hellohellohello"
26+
}
27+
object(MyDateTimeImmutable)#3 (4) {
28+
["myProperty"]=>
29+
object(stdClass)#4 (1) {
30+
["field"]=>
31+
string(15) "hellohellohello"
32+
}
33+
["date"]=>
34+
string(26) "2022-12-22 11:26:00.000000"
35+
["timezone_type"]=>
36+
int(2)
37+
["timezone"]=>
38+
string(1) "Z"
39+
}

0 commit comments

Comments
 (0)