Skip to content

Fixed GH-8471: Segmentation fault when converting immutable and mutable DateTime instances created using reflection #8497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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