From ea954d1a77cbe67b587232f32a760e41d9a1d79e Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 3 Dec 2022 07:33:13 +0000 Subject: [PATCH] intl extension couple of micro optimisations for error edge cases. making c++ compile time few enums ranges. --- ext/intl/breakiterator/breakiterator_methods.cpp | 4 ++-- ext/intl/calendar/calendar_class.cpp | 2 +- ext/intl/calendar/calendar_methods.cpp | 10 +++++----- ext/intl/calendar/gregoriancalendar_methods.cpp | 4 ++-- ext/intl/dateformat/dateformat_attrcpp.cpp | 8 ++++---- ext/intl/dateformat/dateformat_format_object.cpp | 2 +- ext/intl/intl_convertcpp.cpp | 4 ++-- ext/intl/msgformat/msgformat_helpers.cpp | 16 ++++++++-------- ext/intl/timezone/timezone_class.cpp | 4 ++-- ext/intl/timezone/timezone_methods.cpp | 10 +++++----- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/ext/intl/breakiterator/breakiterator_methods.cpp b/ext/intl/breakiterator/breakiterator_methods.cpp index 224b6587fdff..596438bef853 100644 --- a/ext/intl/breakiterator/breakiterator_methods.cpp +++ b/ext/intl/breakiterator/breakiterator_methods.cpp @@ -201,7 +201,7 @@ static void _breakiter_int32_ret_int32( BREAKITER_METHOD_FETCH_OBJECT; - if (arg < INT32_MIN || arg > INT32_MAX) { + if (UNEXPECTED(arg < INT32_MIN || arg > INT32_MAX)) { zend_argument_value_error(1, "must be between %d and %d", INT32_MIN, INT32_MAX); RETURN_THROWS(); } @@ -292,7 +292,7 @@ U_CFUNC PHP_METHOD(IntlBreakIterator, isBoundary) RETURN_THROWS(); } - if (offset < INT32_MIN || offset > INT32_MAX) { + if (UNEXPECTED(offset < INT32_MIN || offset > INT32_MAX)) { zend_argument_value_error(1, "must be between %d and %d", INT32_MIN, INT32_MAX); RETURN_THROWS(); } diff --git a/ext/intl/calendar/calendar_class.cpp b/ext/intl/calendar/calendar_class.cpp index 8a2c7904a2d2..556b5df208fd 100644 --- a/ext/intl/calendar/calendar_class.cpp +++ b/ext/intl/calendar/calendar_class.cpp @@ -94,7 +94,7 @@ static zend_object *Calendar_clone_obj(zend_object *object) Calendar *newCalendar; newCalendar = co_orig->ucal->clone(); - if (!newCalendar) { + if (UNEXPECTED(!newCalendar)) { zend_string *err_msg; intl_errors_set_code(CALENDAR_ERROR_P(co_orig), U_MEMORY_ALLOCATION_ERROR); diff --git a/ext/intl/calendar/calendar_methods.cpp b/ext/intl/calendar/calendar_methods.cpp index 52d1b2cb3037..19b12e6e2261 100644 --- a/ext/intl/calendar/calendar_methods.cpp +++ b/ext/intl/calendar/calendar_methods.cpp @@ -50,7 +50,7 @@ using icu::Locale; } #define ZEND_VALUE_ERROR_OUT_OF_BOUND_VALUE(argument, zpp_arg_position) \ - if (argument < INT32_MIN || argument > INT32_MAX) { \ + if (UNEXPECTED(argument < INT32_MIN || argument > INT32_MAX)) { \ zend_argument_value_error(getThis() ? ((zpp_arg_position)-1) : zpp_arg_position, \ "must be between %d and %d", INT32_MIN, INT32_MAX); \ RETURN_THROWS(); \ @@ -96,7 +96,7 @@ U_CFUNC PHP_FUNCTION(intlcal_create_instance) Calendar *cal = Calendar::createInstance(timeZone, Locale::createFromName(locale_str), status); - if (cal == NULL) { + if (UNEXPECTED(cal == NULL)) { delete timeZone; intl_error_set(NULL, status, "Error creating ICU Calendar object", 0); RETURN_NULL(); @@ -637,7 +637,7 @@ U_CFUNC PHP_FUNCTION(intlcal_get_time_zone) CALENDAR_METHOD_FETCH_OBJECT; TimeZone *tz = co->ucal->getTimeZone().clone(); - if (tz == NULL) { + if (UNEXPECTED(tz == NULL)) { intl_errors_set(CALENDAR_ERROR_P(co), U_MEMORY_ALLOCATION_ERROR, "intlcal_get_time_zone: could not clone TimeZone", 0); RETURN_FALSE; @@ -1000,7 +1000,7 @@ U_CFUNC PHP_FUNCTION(intlcal_from_date_time) cal = Calendar::createInstance(timeZone, Locale::createFromName(locale_str), status); - if (cal == NULL) { + if (UNEXPECTED(cal == NULL)) { delete timeZone; intl_error_set(NULL, status, "intlcal_from_date_time: " "error creating ICU Calendar object", 0); @@ -1045,7 +1045,7 @@ U_CFUNC PHP_FUNCTION(intlcal_to_date_time) INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed"); - if (date > (double)U_INT64_MAX || date < (double)U_INT64_MIN) { + if (UNEXPECTED(date > (double)U_INT64_MAX || date < (double)U_INT64_MIN)) { intl_errors_set(CALENDAR_ERROR_P(co), U_ILLEGAL_ARGUMENT_ERROR, "intlcal_to_date_time: The calendar date is out of the " "range for a 64-bit integer", 0); diff --git a/ext/intl/calendar/gregoriancalendar_methods.cpp b/ext/intl/calendar/gregoriancalendar_methods.cpp index 757b280cd972..014abef3f0c5 100644 --- a/ext/intl/calendar/gregoriancalendar_methods.cpp +++ b/ext/intl/calendar/gregoriancalendar_methods.cpp @@ -135,7 +135,7 @@ static void _php_intlgregcal_constructor_body( } else { // From date/time (3, 5 or 6 arguments) for (int i = 0; i < variant; i++) { - if (largs[i] < INT32_MIN || largs[i] > INT32_MAX) { + if (UNEXPECTED(largs[i] < INT32_MIN || largs[i] > INT32_MAX)) { zend_argument_value_error(getThis() ? (i-1) : i, "must be between %d and %d", INT32_MIN, INT32_MAX); RETURN_THROWS(); @@ -251,7 +251,7 @@ U_CFUNC PHP_FUNCTION(intlgregcal_is_leap_year) RETURN_THROWS(); } - if (year < INT32_MIN || year > INT32_MAX) { + if (UNEXPECTED(year < INT32_MIN || year > INT32_MAX)) { zend_argument_value_error(getThis() ? 1 : 2, "must be between %d and %d", INT32_MIN, INT32_MAX); RETURN_THROWS(); } diff --git a/ext/intl/dateformat/dateformat_attrcpp.cpp b/ext/intl/dateformat/dateformat_attrcpp.cpp index da74b211635c..a9b9a3d8f507 100644 --- a/ext/intl/dateformat/dateformat_attrcpp.cpp +++ b/ext/intl/dateformat/dateformat_attrcpp.cpp @@ -69,7 +69,7 @@ U_CFUNC PHP_FUNCTION(datefmt_get_timezone) const TimeZone& tz = fetch_datefmt(dfo)->getTimeZone(); TimeZone *tz_clone = tz.clone(); - if (tz_clone == NULL) { + if (UNEXPECTED(tz_clone == NULL)) { intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR, "datefmt_get_timezone: Out of memory when cloning time zone", 0); @@ -142,7 +142,7 @@ U_CFUNC PHP_FUNCTION(datefmt_get_calendar_object) } Calendar *cal_clone = cal->clone(); - if (cal_clone == NULL) { + if (UNEXPECTED(cal_clone == NULL)) { intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR, "datefmt_get_calendar_object: Out of memory when cloning " "calendar", 0); @@ -193,7 +193,7 @@ U_CFUNC PHP_FUNCTION(datefmt_set_calendar) if (cal_owned) { /* a non IntlCalendar was specified, we want to keep the timezone */ TimeZone *old_timezone = fetch_datefmt(dfo)->getTimeZone().clone(); - if (old_timezone == NULL) { + if (UNEXPECTED(old_timezone == NULL)) { intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR, "datefmt_set_calendar: Out of memory when cloning calendar", 0); @@ -203,7 +203,7 @@ U_CFUNC PHP_FUNCTION(datefmt_set_calendar) cal->adoptTimeZone(old_timezone); } else { cal = cal->clone(); - if (cal == NULL) { + if (UNEXPECTED(cal == NULL)) { intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR, "datefmt_set_calendar: Out of memory when cloning calendar", 0); diff --git a/ext/intl/dateformat/dateformat_format_object.cpp b/ext/intl/dateformat/dateformat_format_object.cpp index 9e4697902e62..27d39866bc2e 100644 --- a/ext/intl/dateformat/dateformat_format_object.cpp +++ b/ext/intl/dateformat/dateformat_format_object.cpp @@ -37,7 +37,7 @@ using icu::GregorianCalendar; using icu::StringPiece; using icu::SimpleDateFormat; -static const DateFormat::EStyle valid_styles[] = { +static constexpr DateFormat::EStyle valid_styles[] = { DateFormat::kNone, DateFormat::kFull, DateFormat::kLong, diff --git a/ext/intl/intl_convertcpp.cpp b/ext/intl/intl_convertcpp.cpp index 8ea0a0f36d1e..43d0d2cdd385 100644 --- a/ext/intl/intl_convertcpp.cpp +++ b/ext/intl/intl_convertcpp.cpp @@ -23,7 +23,7 @@ extern "C" { /* {{{ intl_stringFromChar */ int intl_stringFromChar(UnicodeString &ret, char *str, size_t str_len, UErrorCode *status) { - if(str_len > INT32_MAX) { + if(UNEXPECTED(str_len > INT32_MAX)) { *status = U_BUFFER_OVERFLOW_ERROR; ret.setToBogus(); return FAILURE; @@ -56,7 +56,7 @@ zend_string* intl_charFromString(const UnicodeString &from, UErrorCode *status) { zend_string *u8res; - if (from.isBogus()) { + if (UNEXPECTED(from.isBogus())) { return NULL; } diff --git a/ext/intl/msgformat/msgformat_helpers.cpp b/ext/intl/msgformat/msgformat_helpers.cpp index 430d5a741115..fbd85b857f3b 100644 --- a/ext/intl/msgformat/msgformat_helpers.cpp +++ b/ext/intl/msgformat/msgformat_helpers.cpp @@ -328,7 +328,7 @@ static void umsg_set_timezone(MessageFormatter_object *mfo, formats = mf->getFormats(count); - if (formats == NULL) { + if (UNEXPECTED(formats == NULL)) { intl_errors_set(&err, U_MEMORY_ALLOCATION_ERROR, "Out of memory retrieving subformats", 0); } @@ -403,7 +403,7 @@ U_CFUNC void umsg_format_helper(MessageFormatter_object *mfo, /* Process key and retrieve type */ if (str_index == NULL) { /* includes case where index < 0 because it's exposed as unsigned */ - if (num_index > (zend_ulong)INT32_MAX) { + if (UNEXPECTED(num_index > (zend_ulong)INT32_MAX)) { intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, "Found negative or too large array key", 0); continue; @@ -477,8 +477,8 @@ U_CFUNC void umsg_format_helper(MessageFormatter_object *mfo, int32_t tInt32 = 0; if (Z_TYPE_P(elem) == IS_DOUBLE) { - if (Z_DVAL_P(elem) > (double)INT32_MAX || - Z_DVAL_P(elem) < (double)INT32_MIN) { + if (UNEXPECTED(Z_DVAL_P(elem) > (double)INT32_MAX || + Z_DVAL_P(elem) < (double)INT32_MIN)) { intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, "Found PHP float with absolute value too large for " "32 bit integer argument", 0); @@ -486,8 +486,8 @@ U_CFUNC void umsg_format_helper(MessageFormatter_object *mfo, tInt32 = (int32_t)Z_DVAL_P(elem); } } else if (Z_TYPE_P(elem) == IS_LONG) { - if (Z_LVAL_P(elem) > INT32_MAX || - Z_LVAL_P(elem) < INT32_MIN) { + if (UNEXPECTED(Z_LVAL_P(elem) > INT32_MAX || + Z_LVAL_P(elem) < INT32_MIN)) { intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, "Found PHP integer with absolute value too large " "for 32 bit integer argument", 0); @@ -505,8 +505,8 @@ U_CFUNC void umsg_format_helper(MessageFormatter_object *mfo, int64_t tInt64 = 0; if (Z_TYPE_P(elem) == IS_DOUBLE) { - if (Z_DVAL_P(elem) > (double)U_INT64_MAX || - Z_DVAL_P(elem) < (double)U_INT64_MIN) { + if (UNEXPECTED(Z_DVAL_P(elem) > (double)U_INT64_MAX || + Z_DVAL_P(elem) < (double)U_INT64_MIN)) { intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, "Found PHP float with absolute value too large for " "64 bit integer argument", 0); diff --git a/ext/intl/timezone/timezone_class.cpp b/ext/intl/timezone/timezone_class.cpp index df1da4c021da..8afc7e2bc4a3 100644 --- a/ext/intl/timezone/timezone_class.cpp +++ b/ext/intl/timezone/timezone_class.cpp @@ -157,7 +157,7 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, return NULL; } timeZone = to->utimezone->clone(); - if (timeZone == NULL) { + 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); @@ -193,7 +193,7 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, return NULL; } timeZone = TimeZone::createTimeZone(id); - if (timeZone == NULL) { + 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); diff --git a/ext/intl/timezone/timezone_methods.cpp b/ext/intl/timezone/timezone_methods.cpp index ef73b9416e9b..cae64026fa5d 100644 --- a/ext/intl/timezone/timezone_methods.cpp +++ b/ext/intl/timezone/timezone_methods.cpp @@ -148,8 +148,8 @@ U_CFUNC PHP_FUNCTION(intltz_create_enumeration) se = TimeZone::createEnumeration(); } else if (Z_TYPE_P(arg) == IS_LONG) { int_offset: - if (Z_LVAL_P(arg) < (zend_long)INT32_MIN || - Z_LVAL_P(arg) > (zend_long)INT32_MAX) { + if (UNEXPECTED(Z_LVAL_P(arg) < (zend_long)INT32_MIN || + Z_LVAL_P(arg) > (zend_long)INT32_MAX)) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_create_enumeration: value is out of range", 0); RETURN_FALSE; @@ -241,7 +241,7 @@ U_CFUNC PHP_FUNCTION(intltz_create_time_zone_id_enumeration) } if (!arg3isnull) { - if (offset_arg < (zend_long)INT32_MIN || offset_arg > (zend_long)INT32_MAX) { + if (UNEXPECTED(offset_arg < (zend_long)INT32_MIN || offset_arg > (zend_long)INT32_MAX)) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_create_time_zone_id_enumeration: offset out of bounds", 0); RETURN_FALSE; @@ -350,7 +350,7 @@ U_CFUNC PHP_FUNCTION(intltz_get_equivalent_id) RETURN_THROWS(); } - if (index < (zend_long)INT32_MIN || index > (zend_long)INT32_MAX) { + if (UNEXPECTED(index < (zend_long)INT32_MIN || index > (zend_long)INT32_MAX)) { RETURN_FALSE; } @@ -475,7 +475,7 @@ U_CFUNC PHP_FUNCTION(intltz_has_same_rules) RETURN_BOOL(to->utimezone->hasSameRules(*other_to->utimezone)); } -static const TimeZone::EDisplayType display_types[] = { +static constexpr TimeZone::EDisplayType display_types[] = { TimeZone::SHORT, TimeZone::LONG, TimeZone::SHORT_GENERIC, TimeZone::LONG_GENERIC, TimeZone::SHORT_GMT, TimeZone::LONG_GMT,