From b3c6b540a1a8f55de4c624b5f29051d8854fca01 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 20 May 2022 15:01:13 +0100 Subject: [PATCH 1/5] Fixed bug #73239 (DateTime shows strange error message with invalid timezone) --- ext/date/php_date.c | 4 ++-- ext/date/tests/bug73239.phpt | 14 ++++++++++++++ ext/date/tests/ini_set_incorrect.phpt | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 ext/date/tests/bug73239.phpt diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 98408ad245ba6..13be4a84c8d56 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -486,7 +486,7 @@ static PHP_INI_MH(OnUpdate_date_timezone) if (stage == PHP_INI_STAGE_RUNTIME) { if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) { if (DATEG(default_timezone) && *DATEG(default_timezone)) { - php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone)); + php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s'", DATEG(default_timezone)); } } else { DATEG(timezone_valid) = 1; @@ -519,7 +519,7 @@ static char* guess_timezone(const timelib_tzdb *tzdb) } if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) { - php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone)); + php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s'", DATEG(default_timezone)); return "UTC"; } diff --git a/ext/date/tests/bug73239.phpt b/ext/date/tests/bug73239.phpt new file mode 100644 index 0000000000000..dd1010852511d --- /dev/null +++ b/ext/date/tests/bug73239.phpt @@ -0,0 +1,14 @@ +--TEST-- +Bug #73239 (Odd warning/exception message with invalid timezones) +--FILE-- +getMessage(), "\n"; +} +?> +--EXPECTF-- +Warning: ini_set(): Invalid date.timezone value 'dummy' in %sbug73239.php on line %d +DateTime::__construct(): Invalid date.timezone value 'dummy', we selected the timezone 'UTC' for now. diff --git a/ext/date/tests/ini_set_incorrect.phpt b/ext/date/tests/ini_set_incorrect.phpt index 1077333591087..47e23a073a815 100644 --- a/ext/date/tests/ini_set_incorrect.phpt +++ b/ext/date/tests/ini_set_incorrect.phpt @@ -7,4 +7,4 @@ ini_set("date.timezone", "Incorrect/Zone"); ?> --EXPECTF-- -Warning: ini_set(): Invalid date.timezone value 'Incorrect/Zone', we selected the timezone 'UTC' for now. in %sini_set_incorrect.php on line %d +Warning: ini_set(): Invalid date.timezone value 'Incorrect/Zone' in %sini_set_incorrect.php on line %d From e0d21297fe2b5866d84066ebf21b46c76f8a7008 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 27 May 2022 10:19:08 +0100 Subject: [PATCH 2/5] Update wording of warning message, and reinstate 'UTC' fallback for ctor --- ext/date/php_date.c | 2 +- ext/date/tests/bug73239.phpt | 2 +- ext/date/tests/date_default_timezone_get-4.phpt | 2 +- ext/intl/tests/dateformat_invalid_timezone.phpt | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 13be4a84c8d56..ed0880e225461 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -519,7 +519,7 @@ static char* guess_timezone(const timelib_tzdb *tzdb) } if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) { - php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s'", DATEG(default_timezone)); + php_error_docref(NULL, E_WARNING, "UTC was used as timezone, because the date.timezone value '%s' is invalid", DATEG(default_timezone)); return "UTC"; } diff --git a/ext/date/tests/bug73239.phpt b/ext/date/tests/bug73239.phpt index dd1010852511d..079ae31d3f443 100644 --- a/ext/date/tests/bug73239.phpt +++ b/ext/date/tests/bug73239.phpt @@ -11,4 +11,4 @@ try { ?> --EXPECTF-- Warning: ini_set(): Invalid date.timezone value 'dummy' in %sbug73239.php on line %d -DateTime::__construct(): Invalid date.timezone value 'dummy', we selected the timezone 'UTC' for now. +DateTime::__construct(): UTC was used as timezone, because the date.timezone value 'dummy' is invalid diff --git a/ext/date/tests/date_default_timezone_get-4.phpt b/ext/date/tests/date_default_timezone_get-4.phpt index f2319856d8ee2..1d56bcb265ee4 100644 --- a/ext/date/tests/date_default_timezone_get-4.phpt +++ b/ext/date/tests/date_default_timezone_get-4.phpt @@ -7,5 +7,5 @@ date.timezone=Incorrect/Zone echo date_default_timezone_get(), "\n"; ?> --EXPECTF-- -Warning: date_default_timezone_get(): Invalid date.timezone value 'Incorrect/Zone', we selected the timezone 'UTC' for now. in %sdate_default_timezone_get-4.php on line %d +Warning: date_default_timezone_get(): UTC was used as timezone, because the date.timezone value 'Incorrect/Zone' is invalid in %sdate_default_timezone_get-4.php on line %d UTC diff --git a/ext/intl/tests/dateformat_invalid_timezone.phpt b/ext/intl/tests/dateformat_invalid_timezone.phpt index f5f08c1b8eab6..dcb3fd46c2701 100644 --- a/ext/intl/tests/dateformat_invalid_timezone.phpt +++ b/ext/intl/tests/dateformat_invalid_timezone.phpt @@ -15,4 +15,4 @@ try { } ?> --EXPECT-- -IntlDateFormatter::__construct(): Invalid date.timezone value 'Mars/Utopia_Planitia', we selected the timezone 'UTC' for now. +IntlDateFormatter::__construct(): UTC was used as timezone, because the date.timezone value 'Mars/Utopia_Planitia' is invalid From 6770158d474fa442ce55bba4ec32dd2703828b33 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 27 May 2022 13:47:52 +0100 Subject: [PATCH 3/5] Only warn when an incorrect timezone is set through 'date.timezone' --- ext/date/php_date.c | 20 +++++++++---------- ext/date/tests/bug73239.phpt | 3 +-- .../tests/date_default_timezone_get-1.phpt | 1 + .../tests/date_default_timezone_get-2.phpt | 1 + .../tests/date_default_timezone_get-4.phpt | 2 +- .../tests/date_default_timezone_set-1.phpt | 3 ++- ext/date/tests/ini_set_incorrect.phpt | 2 +- .../tests/dateformat_invalid_timezone.phpt | 5 +++-- sapi/cli/tests/006.phpt | 2 +- 9 files changed, 20 insertions(+), 19 deletions(-) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index ed0880e225461..d9aa2a08d9cc7 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -224,7 +224,7 @@ static PHP_INI_MH(OnUpdate_date_timezone); /* {{{ INI Settings */ PHP_INI_BEGIN() - STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals) + STD_PHP_INI_ENTRY("date.timezone", "UTC", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals) PHP_INI_ENTRY("date.default_latitude", DATE_DEFAULT_LATITUDE, PHP_INI_ALL, NULL) PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL) PHP_INI_ENTRY("date.sunset_zenith", DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL) @@ -478,21 +478,19 @@ timelib_tzinfo *php_date_parse_tzfile_wrapper(const char *formal_tzname, const t /* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */ static PHP_INI_MH(OnUpdate_date_timezone) { - if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) { + DATEG(timezone_valid) = 0; + + if (new_value && ZSTR_VAL(new_value) && !timelib_timezone_id_is_valid(ZSTR_VAL(new_value), DATE_TIMEZONEDB)) { + php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', using 'UTC' instead", ZSTR_VAL(new_value)); return FAILURE; } - DATEG(timezone_valid) = 0; - if (stage == PHP_INI_STAGE_RUNTIME) { - if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) { - if (DATEG(default_timezone) && *DATEG(default_timezone)) { - php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s'", DATEG(default_timezone)); - } - } else { - DATEG(timezone_valid) = 1; - } + if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) { + return FAILURE; } + DATEG(timezone_valid) = 1; + return SUCCESS; } /* }}} */ diff --git a/ext/date/tests/bug73239.phpt b/ext/date/tests/bug73239.phpt index 079ae31d3f443..ea5eec4bdd7dd 100644 --- a/ext/date/tests/bug73239.phpt +++ b/ext/date/tests/bug73239.phpt @@ -10,5 +10,4 @@ try { } ?> --EXPECTF-- -Warning: ini_set(): Invalid date.timezone value 'dummy' in %sbug73239.php on line %d -DateTime::__construct(): UTC was used as timezone, because the date.timezone value 'dummy' is invalid +Warning: ini_set(): Invalid date.timezone value 'dummy', using 'UTC' instead in %sbug73239.php on line %d diff --git a/ext/date/tests/date_default_timezone_get-1.phpt b/ext/date/tests/date_default_timezone_get-1.phpt index fc466d411d1ec..3164ba7554ee6 100644 --- a/ext/date/tests/date_default_timezone_get-1.phpt +++ b/ext/date/tests/date_default_timezone_get-1.phpt @@ -13,5 +13,6 @@ date.timezone= echo date('e'), "\n"; ?> --EXPECT-- +Warning: PHP Startup: Invalid date.timezone value '', using 'UTC' instead in Unknown on line 0 UTC UTC diff --git a/ext/date/tests/date_default_timezone_get-2.phpt b/ext/date/tests/date_default_timezone_get-2.phpt index 44d94cd76f2a3..d79d8de07a4a2 100644 --- a/ext/date/tests/date_default_timezone_get-2.phpt +++ b/ext/date/tests/date_default_timezone_get-2.phpt @@ -12,4 +12,5 @@ date.timezone= echo date_default_timezone_get(), "\n"; ?> --EXPECT-- +Warning: PHP Startup: Invalid date.timezone value '', using 'UTC' instead in Unknown on line 0 UTC diff --git a/ext/date/tests/date_default_timezone_get-4.phpt b/ext/date/tests/date_default_timezone_get-4.phpt index 1d56bcb265ee4..dc9fac3ee9070 100644 --- a/ext/date/tests/date_default_timezone_get-4.phpt +++ b/ext/date/tests/date_default_timezone_get-4.phpt @@ -7,5 +7,5 @@ date.timezone=Incorrect/Zone echo date_default_timezone_get(), "\n"; ?> --EXPECTF-- -Warning: date_default_timezone_get(): UTC was used as timezone, because the date.timezone value 'Incorrect/Zone' is invalid in %sdate_default_timezone_get-4.php on line %d +Warning: PHP Startup: Invalid date.timezone value 'Incorrect/Zone', using 'UTC' instead in %s on line %d UTC diff --git a/ext/date/tests/date_default_timezone_set-1.phpt b/ext/date/tests/date_default_timezone_set-1.phpt index 54f1fa73f3b00..d4e5d9157ceaa 100644 --- a/ext/date/tests/date_default_timezone_set-1.phpt +++ b/ext/date/tests/date_default_timezone_set-1.phpt @@ -21,7 +21,8 @@ date.timezone= echo date(DATE_ISO8601, $date3), "\n"; echo date(DATE_ISO8601, $date4), "\n"; ?> ---EXPECT-- +--EXPECTF-- +Warning: PHP Startup: Invalid date.timezone value '', using 'UTC' instead in %s on line %d America/Indiana/Knox 2005-01-12T03:00:00-0500 2005-07-12T03:00:00-0500 diff --git a/ext/date/tests/ini_set_incorrect.phpt b/ext/date/tests/ini_set_incorrect.phpt index 47e23a073a815..d5746883fcfba 100644 --- a/ext/date/tests/ini_set_incorrect.phpt +++ b/ext/date/tests/ini_set_incorrect.phpt @@ -7,4 +7,4 @@ ini_set("date.timezone", "Incorrect/Zone"); ?> --EXPECTF-- -Warning: ini_set(): Invalid date.timezone value 'Incorrect/Zone' in %sini_set_incorrect.php on line %d +Warning: ini_set(): Invalid date.timezone value 'Incorrect/Zone', using 'UTC' instead in %sini_set_incorrect.php on line %d diff --git a/ext/intl/tests/dateformat_invalid_timezone.phpt b/ext/intl/tests/dateformat_invalid_timezone.phpt index dcb3fd46c2701..f618502c2fd00 100644 --- a/ext/intl/tests/dateformat_invalid_timezone.phpt +++ b/ext/intl/tests/dateformat_invalid_timezone.phpt @@ -14,5 +14,6 @@ try { echo $e->getMessage(); } ?> ---EXPECT-- -IntlDateFormatter::__construct(): UTC was used as timezone, because the date.timezone value 'Mars/Utopia_Planitia' is invalid +--EXPECTF-- +Warning: PHP Startup: Invalid date.timezone value 'Mars/Utopia_Planitia', using 'UTC' instead in %s on line %d +Wat? diff --git a/sapi/cli/tests/006.phpt b/sapi/cli/tests/006.phpt index 4f33e37c71089..1f2af9c6b6358 100644 --- a/sapi/cli/tests/006.phpt +++ b/sapi/cli/tests/006.phpt @@ -11,7 +11,7 @@ if (PCRE_JIT_SUPPORT == false) { } ?> --INI-- -date.timezone= +date.timezone=UTC --FILE-- Date: Sun, 29 May 2022 17:30:29 +0100 Subject: [PATCH 4/5] Remove no longer used 'timezone_valid' flag --- ext/date/php_date.c | 15 --------------- ext/date/php_date.h | 1 - 2 files changed, 16 deletions(-) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index d9aa2a08d9cc7..d6973ad6117f3 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -342,7 +342,6 @@ static PHP_GINIT_FUNCTION(date) date_globals->default_timezone = NULL; date_globals->timezone = NULL; date_globals->tzcache = NULL; - date_globals->timezone_valid = 0; } /* }}} */ @@ -478,8 +477,6 @@ timelib_tzinfo *php_date_parse_tzfile_wrapper(const char *formal_tzname, const t /* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */ static PHP_INI_MH(OnUpdate_date_timezone) { - DATEG(timezone_valid) = 0; - if (new_value && ZSTR_VAL(new_value) && !timelib_timezone_id_is_valid(ZSTR_VAL(new_value), DATE_TIMEZONEDB)) { php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', using 'UTC' instead", ZSTR_VAL(new_value)); return FAILURE; @@ -489,8 +486,6 @@ static PHP_INI_MH(OnUpdate_date_timezone) return FAILURE; } - DATEG(timezone_valid) = 1; - return SUCCESS; } /* }}} */ @@ -512,16 +507,6 @@ static char* guess_timezone(const timelib_tzdb *tzdb) return Z_STRVAL_P(ztz); } } else if (*DATEG(default_timezone)) { - if (DATEG(timezone_valid) == 1) { - return DATEG(default_timezone); - } - - if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) { - php_error_docref(NULL, E_WARNING, "UTC was used as timezone, because the date.timezone value '%s' is invalid", DATEG(default_timezone)); - return "UTC"; - } - - DATEG(timezone_valid) = 1; return DATEG(default_timezone); } /* Fallback to UTC */ diff --git a/ext/date/php_date.h b/ext/date/php_date.h index 129f3617617a7..a4729ff58ffeb 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -108,7 +108,6 @@ ZEND_BEGIN_MODULE_GLOBALS(date) char *timezone; HashTable *tzcache; timelib_error_container *last_errors; - int timezone_valid; ZEND_END_MODULE_GLOBALS(date) #define DATEG(v) ZEND_MODULE_GLOBALS_ACCESSOR(date, v) From 63aa5a4b5d82961734e24eff6db2adbe6b7b0cfc Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 30 May 2022 19:08:52 +0100 Subject: [PATCH 5/5] Improve error message, and add additional test. --- ext/date/php_date.c | 9 +++++++-- ext/date/tests/ini_set_incorrect-002.phpt | 24 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 ext/date/tests/ini_set_incorrect-002.phpt diff --git a/ext/date/php_date.c b/ext/date/php_date.c index d6973ad6117f3..c7097903f268d 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -478,7 +478,12 @@ timelib_tzinfo *php_date_parse_tzfile_wrapper(const char *formal_tzname, const t static PHP_INI_MH(OnUpdate_date_timezone) { if (new_value && ZSTR_VAL(new_value) && !timelib_timezone_id_is_valid(ZSTR_VAL(new_value), DATE_TIMEZONEDB)) { - php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', using 'UTC' instead", ZSTR_VAL(new_value)); + php_error_docref( + NULL, E_WARNING, + "Invalid date.timezone value '%s', using '%s' instead", + ZSTR_VAL(new_value), + DATEG(default_timezone) ? DATEG(default_timezone) : "UTC" + ); return FAILURE; } @@ -493,7 +498,7 @@ static PHP_INI_MH(OnUpdate_date_timezone) /* {{{ Helper functions */ static char* guess_timezone(const timelib_tzdb *tzdb) { - /* Checking configure timezone */ + /* Checking whether timezone has been set with date_default_timezone_set() */ if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) { return DATEG(timezone); } diff --git a/ext/date/tests/ini_set_incorrect-002.phpt b/ext/date/tests/ini_set_incorrect-002.phpt new file mode 100644 index 0000000000000..b54b1c4bb485b --- /dev/null +++ b/ext/date/tests/ini_set_incorrect-002.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test invalid time zone and defaults +--FILE-- + +--EXPECTF-- +UTC + +Warning: ini_set(): Invalid date.timezone value 'foo', using 'UTC' instead in %sini_set_incorrect-002.php on line %d +UTC +Europe/London + +Warning: ini_set(): Invalid date.timezone value 'Mars/Valles_Marineris', using 'Europe/London' instead in %sini_set_incorrect-002.php on line %d +Europe/London