From 8d4abeae1672ac523a7d1b60dd96bca64667fb07 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 19 Dec 2024 02:12:14 +0000 Subject: [PATCH 1/4] ext/intl: TimeZone address todo to throw exceptions on error. --- .../tests/dateformat_setTimeZone_error.phpt | 20 +++++++----- ext/intl/timezone/timezone_class.cpp | 31 +++---------------- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/ext/intl/tests/dateformat_setTimeZone_error.phpt b/ext/intl/tests/dateformat_setTimeZone_error.phpt index 4467204134062..cafd342e3fb55 100644 --- a/ext/intl/tests/dateformat_setTimeZone_error.phpt +++ b/ext/intl/tests/dateformat_setTimeZone_error.phpt @@ -10,15 +10,19 @@ ini_set("date.timezone", 'Atlantic/Azores'); $df = new IntlDateFormatter(NULL, 0, 0); -var_dump($df->setTimeZone(array())); -var_dump($df->setTimeZone('non existing timezone')); +try { + $df->setTimeZone(array()); +} catch (Throwable $e) { + echo $e->getMessage() . PHP_EOL; +} +try { + $df->setTimeZone('non existing timezone'); +} catch (Throwable $e) { + echo $e->getMessage(); +} ?> --EXPECTF-- Warning: Array to string conversion in %s on line %d - -Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'Array' in %s on line %d -bool(false) - -Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'non existing timezone' in %s on line %d -bool(false) +datefmt_set_timezone: No such time zone: 'Array' +datefmt_set_timezone: No such time zone: 'non existing timezone' diff --git a/ext/intl/timezone/timezone_class.cpp b/ext/intl/timezone/timezone_class.cpp index f7b3d4eeb08f2..bba53c8753a17 100644 --- a/ext/intl/timezone/timezone_class.cpp +++ b/ext/intl/timezone/timezone_class.cpp @@ -147,24 +147,15 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, instanceof_function(Z_OBJCE_P(zv_timezone), TimeZone_ce_ptr)) { TimeZone_object *to = Z_INTL_TIMEZONE_P(zv_timezone); - /* TODO Throw proper Error exceptions for uninitialized classes and failure to clone */ if (to->utimezone == NULL) { - spprintf(&message, 0, "%s: passed IntlTimeZone is not " + zend_throw_error(NULL, "%s: passed IntlTimeZone is not " "properly constructed", func); - if (message) { - intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1); - efree(message); - } zval_ptr_dtor_str(&local_zv_tz); return NULL; } timeZone = to->utimezone->clone(); if (UNEXPECTED(timeZone == NULL)) { - spprintf(&message, 0, "%s: could not clone TimeZone", func); - if (message) { - intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1); - efree(message); - } + zend_throw_error(NULL, "%s: could not clone TimeZone", func); zval_ptr_dtor_str(&local_zv_tz); return NULL; } @@ -185,32 +176,20 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, } if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone), &status) == FAILURE) { - spprintf(&message, 0, "%s: Time zone identifier given is not a " + zend_throw_error(NULL, "%s: Time zone identifier given is not a " "valid UTF-8 string", func); - if (message) { - intl_errors_set(outside_error, status, message, 1); - efree(message); - } zval_ptr_dtor_str(&local_zv_tz); return NULL; } timeZone = TimeZone::createTimeZone(id); if (UNEXPECTED(timeZone == NULL)) { - spprintf(&message, 0, "%s: Could not create time zone", func); - if (message) { - intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1); - efree(message); - } + zend_throw_error(NULL, "%s: Could not create time zone", func); zval_ptr_dtor_str(&local_zv_tz); return NULL; } if (*timeZone == TimeZone::getUnknown()) { - spprintf(&message, 0, "%s: No such time zone: '%s'", + zend_throw_error(NULL, "%s: No such time zone: '%s'", func, Z_STRVAL_P(zv_timezone)); - if (message) { - intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1); - efree(message); - } zval_ptr_dtor_str(&local_zv_tz); delete timeZone; return NULL; From 00dca8f8490bac7287b8366053a5c04f44a524ba Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 21 Dec 2024 18:12:04 +0000 Subject: [PATCH 2/4] update tests --- ext/intl/tests/bug62017.phpt | 17 ++++++++++------- .../tests/calendar_createInstance_error.phpt | 11 +++++++---- .../dateformat___construct_bad_tz_cal.phpt | 3 ++- .../dateformat_set_timezone_id_icu72-1.phpt | 11 +++++++---- ext/intl/timezone/timezone_class.cpp | 11 ++++++----- 5 files changed, 32 insertions(+), 21 deletions(-) diff --git a/ext/intl/tests/bug62017.phpt b/ext/intl/tests/bug62017.phpt index be91594ada521..53b0deb4fa7d8 100644 --- a/ext/intl/tests/bug62017.phpt +++ b/ext/intl/tests/bug62017.phpt @@ -5,19 +5,22 @@ intl --FILE-- getMessage() . " in " . $e->getFile() . " on line " . $e->getLine() . PHP_EOL; +} + try { - var_dump( new IntlDateFormatter('', IntlDateFormatter::NONE, IntlDateFormatter::NONE, "Europe/Lisbon", - IntlDateFormatter::GREGORIAN, "\x80")); + IntlDateFormatter::GREGORIAN, "\x80"); } catch (IntlException $e) { echo PHP_EOL."Exception: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine() . PHP_EOL; } ?> --EXPECTF-- -Warning: datefmt_create(): datefmt_create: Time zone identifier given is not a valid UTF-8 string in %s on line %d -NULL -Exception: IntlDateFormatter::__construct(): datefmt_create: error converting pattern to UTF-16 in %sbug62017.php on line %d +Exception: datefmt_create: Time zone identifier given is not a valid UTF-8 string in %s on line %d + +Exception: IntlDateFormatter::__construct(): datefmt_create: error converting pattern to UTF-16 in %s on line %d diff --git a/ext/intl/tests/calendar_createInstance_error.phpt b/ext/intl/tests/calendar_createInstance_error.phpt index 91c904f32b4c4..a48b6b12c2d13 100644 --- a/ext/intl/tests/calendar_createInstance_error.phpt +++ b/ext/intl/tests/calendar_createInstance_error.phpt @@ -10,8 +10,11 @@ class X extends IntlTimeZone { function __construct() {} } -var_dump(intlcal_create_instance(new X, NULL)); +try { + intlcal_create_instance(new X, NULL); +} catch (IntlException $e) { + echo $e->getMessage(); +} ?> ---EXPECTF-- -Warning: intlcal_create_instance(): intlcal_create_instance: passed IntlTimeZone is not properly constructed in %s on line %d -NULL +--EXPECT-- +intlcal_create_instance: passed IntlTimeZone is not properly constructed diff --git a/ext/intl/tests/dateformat___construct_bad_tz_cal.phpt b/ext/intl/tests/dateformat___construct_bad_tz_cal.phpt index c0c5412e4ab4c..ca7cd5a13dee4 100644 --- a/ext/intl/tests/dateformat___construct_bad_tz_cal.phpt +++ b/ext/intl/tests/dateformat___construct_bad_tz_cal.phpt @@ -29,7 +29,8 @@ try { } ?> --EXPECTF-- -Exception: IntlDateFormatter::__construct(): datefmt_create: No such time zone: 'bad timezone' in %s on line %d + +Exception: datefmt_create: No such time zone: 'bad timezone' in %s on line %d Exception: IntlDateFormatter::__construct(): datefmt_create: Invalid value for calendar type; it must be one of IntlDateFormatter::TRADITIONAL (locale's default calendar) or IntlDateFormatter::GREGORIAN. Alternatively, it can be an IntlCalendar object in %s on line %d diff --git a/ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt b/ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt index 79953ab26b465..bd066aea7ced0 100644 --- a/ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt +++ b/ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt @@ -37,7 +37,11 @@ function ut_main() $res_str .= "-----------"; $res_str .= "\nTrying to set timezone_id= $timezone_id_entry"; - if (ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ) !== $result) die("ut_datefmt_set_timezone_id failed"); + try { + if (ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ) !== $result) die("ut_datefmt_set_timezone_id failed"); + } catch (IntlException $e) { + echo $e->getMessage() . PHP_EOL; + } $timezone_id = ut_datefmt_get_timezone_id( $fmt ); $res_str .= "\nAfter call to set_timezone_id : timezone_id= $timezone_id"; $formatted = ut_datefmt_format( $fmt, 0); @@ -58,9 +62,8 @@ include_once( 'ut_common.inc' ); ut_run(); ?> --EXPECTF-- -Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d - -Warning: datefmt_set_timezone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d +datefmt_set_timezone: No such time zone: 'CN' +datefmt_set_timezone: No such time zone: 'CN' After creation of the dateformatter : timezone_id= US/Pacific ----------- diff --git a/ext/intl/timezone/timezone_class.cpp b/ext/intl/timezone/timezone_class.cpp index bba53c8753a17..9d477aaeca83b 100644 --- a/ext/intl/timezone/timezone_class.cpp +++ b/ext/intl/timezone/timezone_class.cpp @@ -22,6 +22,7 @@ #include #include #include "../intl_convertcpp.h" +#include "../intl_common.h" #include "../common/common_date.h" @@ -148,14 +149,14 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, TimeZone_object *to = Z_INTL_TIMEZONE_P(zv_timezone); if (to->utimezone == NULL) { - zend_throw_error(NULL, "%s: passed IntlTimeZone is not " + zend_throw_error(IntlException_ce_ptr, "%s: passed IntlTimeZone is not " "properly constructed", func); zval_ptr_dtor_str(&local_zv_tz); return NULL; } timeZone = to->utimezone->clone(); if (UNEXPECTED(timeZone == NULL)) { - zend_throw_error(NULL, "%s: could not clone TimeZone", func); + zend_throw_error(IntlException_ce_ptr, "%s: could not clone TimeZone", func); zval_ptr_dtor_str(&local_zv_tz); return NULL; } @@ -176,19 +177,19 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, } if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone), &status) == FAILURE) { - zend_throw_error(NULL, "%s: Time zone identifier given is not a " + zend_throw_error(IntlException_ce_ptr, "%s: Time zone identifier given is not a " "valid UTF-8 string", func); zval_ptr_dtor_str(&local_zv_tz); return NULL; } timeZone = TimeZone::createTimeZone(id); if (UNEXPECTED(timeZone == NULL)) { - zend_throw_error(NULL, "%s: Could not create time zone", func); + zend_throw_error(IntlException_ce_ptr, "%s: Could not create time zone", func); zval_ptr_dtor_str(&local_zv_tz); return NULL; } if (*timeZone == TimeZone::getUnknown()) { - zend_throw_error(NULL, "%s: No such time zone: '%s'", + zend_throw_error(IntlException_ce_ptr, "%s: No such time zone: '%s'", func, Z_STRVAL_P(zv_timezone)); zval_ptr_dtor_str(&local_zv_tz); delete timeZone; From 40986554863c015071b1f82288e95b4a13d8e4e0 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 23 Dec 2024 22:00:13 +0000 Subject: [PATCH 3/4] update tests --- ext/intl/tests/dateformat_setTimeZone_error.phpt | 4 ++-- ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/intl/tests/dateformat_setTimeZone_error.phpt b/ext/intl/tests/dateformat_setTimeZone_error.phpt index cafd342e3fb55..b911c4b41ec0c 100644 --- a/ext/intl/tests/dateformat_setTimeZone_error.phpt +++ b/ext/intl/tests/dateformat_setTimeZone_error.phpt @@ -12,13 +12,13 @@ $df = new IntlDateFormatter(NULL, 0, 0); try { $df->setTimeZone(array()); -} catch (Throwable $e) { +} catch (IntlException $e) { echo $e->getMessage() . PHP_EOL; } try { $df->setTimeZone('non existing timezone'); -} catch (Throwable $e) { +} catch (IntlException $e) { echo $e->getMessage(); } ?> diff --git a/ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt b/ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt index bd066aea7ced0..eebc6f0973a05 100644 --- a/ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt +++ b/ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt @@ -38,7 +38,7 @@ function ut_main() $res_str .= "-----------"; $res_str .= "\nTrying to set timezone_id= $timezone_id_entry"; try { - if (ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ) !== $result) die("ut_datefmt_set_timezone_id failed"); + ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ); } catch (IntlException $e) { echo $e->getMessage() . PHP_EOL; } From fd18a5bcc9f28d0cd7a1586d7ef4d0cd748ede98 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 23 Dec 2024 23:54:22 +0000 Subject: [PATCH 4/4] fix test --- ext/intl/tests/dateformat_set_timezone_id3.phpt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ext/intl/tests/dateformat_set_timezone_id3.phpt b/ext/intl/tests/dateformat_set_timezone_id3.phpt index a964bb620bc1f..16c0bb05b9be3 100644 --- a/ext/intl/tests/dateformat_set_timezone_id3.phpt +++ b/ext/intl/tests/dateformat_set_timezone_id3.phpt @@ -37,7 +37,11 @@ function ut_main() $res_str .= "-----------"; $res_str .= "\nTrying to set timezone_id= $timezone_id_entry"; - if (ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ) !== $result) die("ut_datefmt_set_timezone_id failed"); + try { + ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ); + } catch (IntlException $e) { + echo $e->getMessage() . PHP_EOL; + } $timezone_id = ut_datefmt_get_timezone_id( $fmt ); $res_str .= "\nAfter call to set_timezone_id : timezone_id= $timezone_id"; $formatted = ut_datefmt_format( $fmt, 0); @@ -58,9 +62,8 @@ include_once( 'ut_common.inc' ); ut_run(); ?> --EXPECTF-- -Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d - -Warning: datefmt_set_timezone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d +datefmt_set_timezone: No such time zone: 'CN' +datefmt_set_timezone: No such time zone: 'CN' After creation of the dateformatter : timezone_id= US/Pacific -----------