Skip to content

Commit dadc843

Browse files
committed
Merged pull request #8497
2 parents 710294c + 92f8f19 commit dadc843

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

ext/date/php_date.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,8 +2468,10 @@ PHP_METHOD(DateTime, createFromImmutable)
24682468
Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable)
24692469
ZEND_PARSE_PARAMETERS_END();
24702470

2471-
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
24722471
old_obj = Z_PHPDATE_P(datetimeimmutable_object);
2472+
DATE_CHECK_INITIALIZED(old_obj->time, DateTimeImmutable);
2473+
2474+
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
24732475
new_obj = Z_PHPDATE_P(return_value);
24742476

24752477
new_obj->time = timelib_time_clone(old_obj->time);
@@ -2487,8 +2489,10 @@ PHP_METHOD(DateTime, createFromInterface)
24872489
Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
24882490
ZEND_PARSE_PARAMETERS_END();
24892491

2490-
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
24912492
old_obj = Z_PHPDATE_P(datetimeinterface_object);
2493+
DATE_CHECK_INITIALIZED(old_obj->time, DateTimeInterface);
2494+
2495+
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
24922496
new_obj = Z_PHPDATE_P(return_value);
24932497

24942498
new_obj->time = timelib_time_clone(old_obj->time);
@@ -2506,8 +2510,10 @@ PHP_METHOD(DateTimeImmutable, createFromMutable)
25062510
Z_PARAM_OBJECT_OF_CLASS(datetime_object, date_ce_date)
25072511
ZEND_PARSE_PARAMETERS_END();
25082512

2509-
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
25102513
old_obj = Z_PHPDATE_P(datetime_object);
2514+
DATE_CHECK_INITIALIZED(old_obj->time, DateTime);
2515+
2516+
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
25112517
new_obj = Z_PHPDATE_P(return_value);
25122518

25132519
new_obj->time = timelib_time_clone(old_obj->time);
@@ -2525,8 +2531,10 @@ PHP_METHOD(DateTimeImmutable, createFromInterface)
25252531
Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
25262532
ZEND_PARSE_PARAMETERS_END();
25272533

2528-
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
25292534
old_obj = Z_PHPDATE_P(datetimeinterface_object);
2535+
DATE_CHECK_INITIALIZED(old_obj->time, DateTimeInterface);
2536+
2537+
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
25302538
new_obj = Z_PHPDATE_P(return_value);
25312539

25322540
new_obj->time = timelib_time_clone(old_obj->time);

ext/date/tests/bug-gh8471.phpt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
Bug GH-8471: Segmentation fault when converting immutable and mutable DateTime instances created using reflection
3+
--FILE--
4+
<?php
5+
$reflection = new ReflectionClass('\DateTime');
6+
7+
$mutable = $reflection->newInstanceWithoutConstructor();
8+
try {
9+
$immutable = \DateTimeImmutable::createFromMutable($mutable);
10+
} catch (Throwable $t) {
11+
echo $t->getMessage(), "\n";
12+
}
13+
14+
15+
$reflection = new ReflectionClass('\DateTime');
16+
17+
$mutable = $reflection->newInstanceWithoutConstructor();
18+
try {
19+
$immutable = \DateTimeImmutable::createFromInterface($mutable);
20+
} catch (Throwable $t) {
21+
echo $t->getMessage(), "\n";
22+
}
23+
24+
25+
$reflection = new ReflectionClass('\DateTimeImmutable');
26+
27+
$immutable = $reflection->newInstanceWithoutConstructor();
28+
try {
29+
$mutable = \DateTime::createFromImmutable($immutable);
30+
} catch (Throwable $t) {
31+
echo $t->getMessage(), "\n";
32+
}
33+
34+
35+
$reflection = new ReflectionClass('\DateTimeImmutable');
36+
37+
$immutable = $reflection->newInstanceWithoutConstructor();
38+
try {
39+
$mutable = \DateTime::createFromInterface($immutable);
40+
} catch (Throwable $t) {
41+
echo $t->getMessage(), "\n";
42+
}
43+
44+
45+
?>
46+
--EXPECTF--
47+
The DateTime object has not been correctly initialized by its constructor
48+
The DateTimeInterface object has not been correctly initialized by its constructor
49+
The DateTimeImmutable object has not been correctly initialized by its constructor
50+
The DateTimeInterface object has not been correctly initialized by its constructor

0 commit comments

Comments
 (0)