From 90434d7fe3ac204b56b85cf8ca606e7dd8cf27ac Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 3 Aug 2020 00:41:31 +0100 Subject: [PATCH] Fixed bug #60302: DateTime::createFromFormat should new static(), not new self() Also fixes similar issues for DateTimeImmutable::createFromFormat, DateTime::createFromImmmutable, DateTime::createFromInterface, DateTimeImmutable::createFromMutable, and DateTimeImmutable::createFromInterface. --- NEWS | 4 + ext/date/php_date.c | 14 ++-- ...imeImmutable_createFromInterface-001.phpt} | 0 ...TimeImmutable_createFromInterface-002.phpt | 81 +++++++++++++++++++ ...eTimeImmutable_createFromMutable-001.phpt} | 0 ...teTimeImmutable_createFromMutable-002.phpt | 29 +++++++ ... => DateTime_createFromImmutable-001.phpt} | 2 +- .../DateTime_createFromImmutable-002.phpt | 35 ++++++++ ... => DateTime_createFromInterface-001.phpt} | 0 .../DateTime_createFromInterface-002.phpt | 81 +++++++++++++++++++ ext/date/tests/bug60302-001.phpt | 13 +++ ext/date/tests/bug60302-002.phpt | 13 +++ 12 files changed, 264 insertions(+), 8 deletions(-) rename ext/date/tests/{DateTimeImmutable_createFromInterface.phpt => DateTimeImmutable_createFromInterface-001.phpt} (100%) create mode 100644 ext/date/tests/DateTimeImmutable_createFromInterface-002.phpt rename ext/date/tests/{DateTimeImmutable_createFromMutable.phpt => DateTimeImmutable_createFromMutable-001.phpt} (100%) create mode 100644 ext/date/tests/DateTimeImmutable_createFromMutable-002.phpt rename ext/date/tests/{DateTime_createFromImmutable.phpt => DateTime_createFromImmutable-001.phpt} (94%) create mode 100644 ext/date/tests/DateTime_createFromImmutable-002.phpt rename ext/date/tests/{DateTime_createFromInterface.phpt => DateTime_createFromInterface-001.phpt} (100%) create mode 100644 ext/date/tests/DateTime_createFromInterface-002.phpt create mode 100644 ext/date/tests/bug60302-001.phpt create mode 100644 ext/date/tests/bug60302-002.phpt diff --git a/NEWS b/NEWS index 1dac12226dd..d1ce1ff4a35 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,10 @@ PHP NEWS . Fixed bug #79897 (Promoted constructor params with attribs cause crash). (Deus Kane) +- Date: + . Fixed bug #60302 (DateTime::createFromFormat should new static(), not new + self()). (Derick) + - JIT: . Fixed bug #79864 (JIT segfault in Symfony OptionsResolver). (Dmitry) . Fixed bug #79888 (Incorrect execution with JIT enabled). (Dmitry) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 46d50e28371..9839d768f9b 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -2292,7 +2292,7 @@ PHP_FUNCTION(date_create) } /* }}} */ -/* {{{ Returns new DateTime object */ +/* {{{ Returns new DateTimeImmutable object */ PHP_FUNCTION(date_create_immutable) { zval *timezone_object = NULL; @@ -2327,7 +2327,7 @@ PHP_FUNCTION(date_create_from_format) Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone) ZEND_PARSE_PARAMETERS_END(); - php_date_instantiate(date_ce_date, return_value); + php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value); if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) { zval_ptr_dtor(return_value); RETURN_FALSE; @@ -2349,7 +2349,7 @@ PHP_FUNCTION(date_create_immutable_from_format) Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone) ZEND_PARSE_PARAMETERS_END(); - php_date_instantiate(date_ce_immutable, return_value); + php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value); if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) { zval_ptr_dtor(return_value); RETURN_FALSE; @@ -2408,7 +2408,7 @@ PHP_METHOD(DateTime, createFromImmutable) Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable) ZEND_PARSE_PARAMETERS_END(); - php_date_instantiate(date_ce_date, return_value); + 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); new_obj = Z_PHPDATE_P(return_value); @@ -2427,7 +2427,7 @@ PHP_METHOD(DateTime, createFromInterface) Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface) ZEND_PARSE_PARAMETERS_END(); - php_date_instantiate(date_ce_date, return_value); + 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); new_obj = Z_PHPDATE_P(return_value); @@ -2446,7 +2446,7 @@ PHP_METHOD(DateTimeImmutable, createFromMutable) Z_PARAM_OBJECT_OF_CLASS(datetime_object, date_ce_date) ZEND_PARSE_PARAMETERS_END(); - php_date_instantiate(date_ce_immutable, return_value); + 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); new_obj = Z_PHPDATE_P(return_value); @@ -2465,7 +2465,7 @@ PHP_METHOD(DateTimeImmutable, createFromInterface) Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface) ZEND_PARSE_PARAMETERS_END(); - php_date_instantiate(date_ce_immutable, return_value); + 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); new_obj = Z_PHPDATE_P(return_value); diff --git a/ext/date/tests/DateTimeImmutable_createFromInterface.phpt b/ext/date/tests/DateTimeImmutable_createFromInterface-001.phpt similarity index 100% rename from ext/date/tests/DateTimeImmutable_createFromInterface.phpt rename to ext/date/tests/DateTimeImmutable_createFromInterface-001.phpt diff --git a/ext/date/tests/DateTimeImmutable_createFromInterface-002.phpt b/ext/date/tests/DateTimeImmutable_createFromInterface-002.phpt new file mode 100644 index 00000000000..fcdc64ce4c4 --- /dev/null +++ b/ext/date/tests/DateTimeImmutable_createFromInterface-002.phpt @@ -0,0 +1,81 @@ +--TEST-- +Tests for DateTimeImmutable::createFromInterface inheritance +--INI-- +date.timezone=Europe/London +--FILE-- + +--EXPECTF-- +object(MyDateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(MyDateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(MyDateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:06:46.000000" + ["timezone_type"]=> + int(2) + ["timezone"]=> + string(3) "CET" +} +object(MyDateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:06:46.000000" + ["timezone_type"]=> + int(2) + ["timezone"]=> + string(3) "CET" +} +object(MyDateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:08:20.000000" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(6) "+01:00" +} +object(MyDateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:08:20.000000" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(6) "+01:00" +} diff --git a/ext/date/tests/DateTimeImmutable_createFromMutable.phpt b/ext/date/tests/DateTimeImmutable_createFromMutable-001.phpt similarity index 100% rename from ext/date/tests/DateTimeImmutable_createFromMutable.phpt rename to ext/date/tests/DateTimeImmutable_createFromMutable-001.phpt diff --git a/ext/date/tests/DateTimeImmutable_createFromMutable-002.phpt b/ext/date/tests/DateTimeImmutable_createFromMutable-002.phpt new file mode 100644 index 00000000000..ccbbe33f178 --- /dev/null +++ b/ext/date/tests/DateTimeImmutable_createFromMutable-002.phpt @@ -0,0 +1,29 @@ +--TEST-- +Tests for inherited DateTimeImmutable::createFromMutable +--INI-- +date.timezone=Europe/London +--FILE-- +getMessage(), "\n"; +} +?> +--EXPECTF-- +object(MyDateTimeImmutable)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +DateTimeImmutable::createFromMutable(): Argument #1 ($object) must be of type DateTime, DateTimeImmutable given diff --git a/ext/date/tests/DateTime_createFromImmutable.phpt b/ext/date/tests/DateTime_createFromImmutable-001.phpt similarity index 94% rename from ext/date/tests/DateTime_createFromImmutable.phpt rename to ext/date/tests/DateTime_createFromImmutable-001.phpt index 59f61922141..031c8764847 100644 --- a/ext/date/tests/DateTime_createFromImmutable.phpt +++ b/ext/date/tests/DateTime_createFromImmutable-001.phpt @@ -1,5 +1,5 @@ --TEST-- -Tests for DateTime::createFromImmutable. +Tests for DateTime::createFromImmutable --INI-- date.timezone=Europe/London --FILE-- diff --git a/ext/date/tests/DateTime_createFromImmutable-002.phpt b/ext/date/tests/DateTime_createFromImmutable-002.phpt new file mode 100644 index 00000000000..5b8c3a5a7cf --- /dev/null +++ b/ext/date/tests/DateTime_createFromImmutable-002.phpt @@ -0,0 +1,35 @@ +--TEST-- +Tests for inherited DateTime::createFromImmutable +--INI-- +date.timezone=Europe/London +--FILE-- +modify('+ 1 hour'); + +var_dump( $i->format('Y-m-d H:i:s') === $current ); + +try { + MyDateTime::createFromImmutable( date_create( $current ) ); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} +?> +--EXPECTF-- +object(MyDateTime)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +bool(true) +DateTime::createFromImmutable(): Argument #1 ($object) must be of type DateTimeImmutable, DateTime given diff --git a/ext/date/tests/DateTime_createFromInterface.phpt b/ext/date/tests/DateTime_createFromInterface-001.phpt similarity index 100% rename from ext/date/tests/DateTime_createFromInterface.phpt rename to ext/date/tests/DateTime_createFromInterface-001.phpt diff --git a/ext/date/tests/DateTime_createFromInterface-002.phpt b/ext/date/tests/DateTime_createFromInterface-002.phpt new file mode 100644 index 00000000000..6e241a42c92 --- /dev/null +++ b/ext/date/tests/DateTime_createFromInterface-002.phpt @@ -0,0 +1,81 @@ +--TEST-- +Tests for DateTime::createFromInterface with inheritance +--INI-- +date.timezone=Europe/London +--FILE-- + +--EXPECTF-- +object(MyDateTime)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(MyDateTime)#%d (3) { + ["date"]=> + string(26) "2014-03-02 16:24:08.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +object(MyDateTime)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:06:46.000000" + ["timezone_type"]=> + int(2) + ["timezone"]=> + string(3) "CET" +} +object(MyDateTime)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:06:46.000000" + ["timezone_type"]=> + int(2) + ["timezone"]=> + string(3) "CET" +} +object(MyDateTime)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:08:20.000000" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(6) "+01:00" +} +object(MyDateTime)#%d (3) { + ["date"]=> + string(26) "2019-12-16 15:08:20.000000" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(6) "+01:00" +} diff --git a/ext/date/tests/bug60302-001.phpt b/ext/date/tests/bug60302-001.phpt new file mode 100644 index 00000000000..f86443e1a6e --- /dev/null +++ b/ext/date/tests/bug60302-001.phpt @@ -0,0 +1,13 @@ +--TEST-- +Test for bug #60302: DateTime::createFromFormat should new static(), not new self() +--FILE-- + +--EXPECT-- +MyDateTime diff --git a/ext/date/tests/bug60302-002.phpt b/ext/date/tests/bug60302-002.phpt new file mode 100644 index 00000000000..a4a3c4a63ad --- /dev/null +++ b/ext/date/tests/bug60302-002.phpt @@ -0,0 +1,13 @@ +--TEST-- +Test for bug #60302: DateTimeImmutable::createFromFormat should new static(), not new self() +--FILE-- + +--EXPECT-- +MyDateTime