intl extension couple of micro optimisations for error edge cases. (#10044)

making c++ compile time few enums ranges.
This commit is contained in:
David CARLIER 2023-01-14 07:26:05 +00:00 committed by GitHub
parent 9788244a42
commit 690db97c6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 32 additions and 32 deletions

View file

@ -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();
}

View file

@ -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);

View file

@ -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);

View file

@ -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();
}

View file

@ -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);

View file

@ -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,

View file

@ -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;
}

View file

@ -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);

View file

@ -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);

View file

@ -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,