Skip to content

Commit 7670ee4

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
2 parents ec92130 + dadc843 commit 7670ee4

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
@@ -2459,8 +2459,10 @@ PHP_METHOD(DateTime, createFromImmutable)
24592459
Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable)
24602460
ZEND_PARSE_PARAMETERS_END();
24612461

2462-
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
24632462
old_obj = Z_PHPDATE_P(datetimeimmutable_object);
2463+
DATE_CHECK_INITIALIZED(old_obj->time, DateTimeImmutable);
2464+
2465+
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
24642466
new_obj = Z_PHPDATE_P(return_value);
24652467

24662468
new_obj->time = timelib_time_clone(old_obj->time);
@@ -2478,8 +2480,10 @@ PHP_METHOD(DateTime, createFromInterface)
24782480
Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
24792481
ZEND_PARSE_PARAMETERS_END();
24802482

2481-
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
24822483
old_obj = Z_PHPDATE_P(datetimeinterface_object);
2484+
DATE_CHECK_INITIALIZED(old_obj->time, DateTimeInterface);
2485+
2486+
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
24832487
new_obj = Z_PHPDATE_P(return_value);
24842488

24852489
new_obj->time = timelib_time_clone(old_obj->time);
@@ -2497,8 +2501,10 @@ PHP_METHOD(DateTimeImmutable, createFromMutable)
24972501
Z_PARAM_OBJECT_OF_CLASS(datetime_object, date_ce_date)
24982502
ZEND_PARSE_PARAMETERS_END();
24992503

2500-
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
25012504
old_obj = Z_PHPDATE_P(datetime_object);
2505+
DATE_CHECK_INITIALIZED(old_obj->time, DateTime);
2506+
2507+
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
25022508
new_obj = Z_PHPDATE_P(return_value);
25032509

25042510
new_obj->time = timelib_time_clone(old_obj->time);
@@ -2516,8 +2522,10 @@ PHP_METHOD(DateTimeImmutable, createFromInterface)
25162522
Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
25172523
ZEND_PARSE_PARAMETERS_END();
25182524

2519-
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
25202525
old_obj = Z_PHPDATE_P(datetimeinterface_object);
2526+
DATE_CHECK_INITIALIZED(old_obj->time, DateTimeInterface);
2527+
2528+
php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
25212529
new_obj = Z_PHPDATE_P(return_value);
25222530

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