Skip to content

Commit

Permalink
Merged pull request #8497
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed May 5, 2022
2 parents 710294c + 92f8f19 commit dadc843
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
16 changes: 12 additions & 4 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -2468,8 +2468,10 @@ PHP_METHOD(DateTime, createFromImmutable)
Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable)
ZEND_PARSE_PARAMETERS_END();

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
old_obj = Z_PHPDATE_P(datetimeimmutable_object);
DATE_CHECK_INITIALIZED(old_obj->time, DateTimeImmutable);

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
new_obj = Z_PHPDATE_P(return_value);

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

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
old_obj = Z_PHPDATE_P(datetimeinterface_object);
DATE_CHECK_INITIALIZED(old_obj->time, DateTimeInterface);

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
new_obj = Z_PHPDATE_P(return_value);

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

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
old_obj = Z_PHPDATE_P(datetime_object);
DATE_CHECK_INITIALIZED(old_obj->time, DateTime);

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
new_obj = Z_PHPDATE_P(return_value);

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

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
old_obj = Z_PHPDATE_P(datetimeinterface_object);
DATE_CHECK_INITIALIZED(old_obj->time, DateTimeInterface);

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
new_obj = Z_PHPDATE_P(return_value);

new_obj->time = timelib_time_clone(old_obj->time);
Expand Down
50 changes: 50 additions & 0 deletions ext/date/tests/bug-gh8471.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
Bug GH-8471: Segmentation fault when converting immutable and mutable DateTime instances created using reflection
--FILE--
<?php
$reflection = new ReflectionClass('\DateTime');

$mutable = $reflection->newInstanceWithoutConstructor();
try {
$immutable = \DateTimeImmutable::createFromMutable($mutable);
} catch (Throwable $t) {
echo $t->getMessage(), "\n";
}


$reflection = new ReflectionClass('\DateTime');

$mutable = $reflection->newInstanceWithoutConstructor();
try {
$immutable = \DateTimeImmutable::createFromInterface($mutable);
} catch (Throwable $t) {
echo $t->getMessage(), "\n";
}


$reflection = new ReflectionClass('\DateTimeImmutable');

$immutable = $reflection->newInstanceWithoutConstructor();
try {
$mutable = \DateTime::createFromImmutable($immutable);
} catch (Throwable $t) {
echo $t->getMessage(), "\n";
}


$reflection = new ReflectionClass('\DateTimeImmutable');

$immutable = $reflection->newInstanceWithoutConstructor();
try {
$mutable = \DateTime::createFromInterface($immutable);
} catch (Throwable $t) {
echo $t->getMessage(), "\n";
}


?>
--EXPECTF--
The DateTime object has not been correctly initialized by its constructor
The DateTimeInterface object has not been correctly initialized by its constructor
The DateTimeImmutable object has not been correctly initialized by its constructor
The DateTimeInterface object has not been correctly initialized by its constructor

0 comments on commit dadc843

Please sign in to comment.