Skip to content

Commit 690db97

Browse files
authored
intl extension couple of micro optimisations for error edge cases. (#10044)
making c++ compile time few enums ranges.
1 parent 9788244 commit 690db97

10 files changed

+32
-32
lines changed

ext/intl/breakiterator/breakiterator_methods.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ static void _breakiter_int32_ret_int32(
201201

202202
BREAKITER_METHOD_FETCH_OBJECT;
203203

204-
if (arg < INT32_MIN || arg > INT32_MAX) {
204+
if (UNEXPECTED(arg < INT32_MIN || arg > INT32_MAX)) {
205205
zend_argument_value_error(1, "must be between %d and %d", INT32_MIN, INT32_MAX);
206206
RETURN_THROWS();
207207
}
@@ -292,7 +292,7 @@ U_CFUNC PHP_METHOD(IntlBreakIterator, isBoundary)
292292
RETURN_THROWS();
293293
}
294294

295-
if (offset < INT32_MIN || offset > INT32_MAX) {
295+
if (UNEXPECTED(offset < INT32_MIN || offset > INT32_MAX)) {
296296
zend_argument_value_error(1, "must be between %d and %d", INT32_MIN, INT32_MAX);
297297
RETURN_THROWS();
298298
}

ext/intl/calendar/calendar_class.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static zend_object *Calendar_clone_obj(zend_object *object)
9494
Calendar *newCalendar;
9595

9696
newCalendar = co_orig->ucal->clone();
97-
if (!newCalendar) {
97+
if (UNEXPECTED(!newCalendar)) {
9898
zend_string *err_msg;
9999
intl_errors_set_code(CALENDAR_ERROR_P(co_orig),
100100
U_MEMORY_ALLOCATION_ERROR);

ext/intl/calendar/calendar_methods.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ using icu::Locale;
5050
}
5151

5252
#define ZEND_VALUE_ERROR_OUT_OF_BOUND_VALUE(argument, zpp_arg_position) \
53-
if (argument < INT32_MIN || argument > INT32_MAX) { \
53+
if (UNEXPECTED(argument < INT32_MIN || argument > INT32_MAX)) { \
5454
zend_argument_value_error(getThis() ? ((zpp_arg_position)-1) : zpp_arg_position, \
5555
"must be between %d and %d", INT32_MIN, INT32_MAX); \
5656
RETURN_THROWS(); \
@@ -96,7 +96,7 @@ U_CFUNC PHP_FUNCTION(intlcal_create_instance)
9696

9797
Calendar *cal = Calendar::createInstance(timeZone,
9898
Locale::createFromName(locale_str), status);
99-
if (cal == NULL) {
99+
if (UNEXPECTED(cal == NULL)) {
100100
delete timeZone;
101101
intl_error_set(NULL, status, "Error creating ICU Calendar object", 0);
102102
RETURN_NULL();
@@ -637,7 +637,7 @@ U_CFUNC PHP_FUNCTION(intlcal_get_time_zone)
637637
CALENDAR_METHOD_FETCH_OBJECT;
638638

639639
TimeZone *tz = co->ucal->getTimeZone().clone();
640-
if (tz == NULL) {
640+
if (UNEXPECTED(tz == NULL)) {
641641
intl_errors_set(CALENDAR_ERROR_P(co), U_MEMORY_ALLOCATION_ERROR,
642642
"intlcal_get_time_zone: could not clone TimeZone", 0);
643643
RETURN_FALSE;
@@ -1000,7 +1000,7 @@ U_CFUNC PHP_FUNCTION(intlcal_from_date_time)
10001000

10011001
cal = Calendar::createInstance(timeZone,
10021002
Locale::createFromName(locale_str), status);
1003-
if (cal == NULL) {
1003+
if (UNEXPECTED(cal == NULL)) {
10041004
delete timeZone;
10051005
intl_error_set(NULL, status, "intlcal_from_date_time: "
10061006
"error creating ICU Calendar object", 0);
@@ -1045,7 +1045,7 @@ U_CFUNC PHP_FUNCTION(intlcal_to_date_time)
10451045

10461046
INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed");
10471047

1048-
if (date > (double)U_INT64_MAX || date < (double)U_INT64_MIN) {
1048+
if (UNEXPECTED(date > (double)U_INT64_MAX || date < (double)U_INT64_MIN)) {
10491049
intl_errors_set(CALENDAR_ERROR_P(co), U_ILLEGAL_ARGUMENT_ERROR,
10501050
"intlcal_to_date_time: The calendar date is out of the "
10511051
"range for a 64-bit integer", 0);

ext/intl/calendar/gregoriancalendar_methods.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static void _php_intlgregcal_constructor_body(
135135
} else {
136136
// From date/time (3, 5 or 6 arguments)
137137
for (int i = 0; i < variant; i++) {
138-
if (largs[i] < INT32_MIN || largs[i] > INT32_MAX) {
138+
if (UNEXPECTED(largs[i] < INT32_MIN || largs[i] > INT32_MAX)) {
139139
zend_argument_value_error(getThis() ? (i-1) : i,
140140
"must be between %d and %d", INT32_MIN, INT32_MAX);
141141
RETURN_THROWS();
@@ -251,7 +251,7 @@ U_CFUNC PHP_FUNCTION(intlgregcal_is_leap_year)
251251
RETURN_THROWS();
252252
}
253253

254-
if (year < INT32_MIN || year > INT32_MAX) {
254+
if (UNEXPECTED(year < INT32_MIN || year > INT32_MAX)) {
255255
zend_argument_value_error(getThis() ? 1 : 2, "must be between %d and %d", INT32_MIN, INT32_MAX);
256256
RETURN_THROWS();
257257
}

ext/intl/dateformat/dateformat_attrcpp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ U_CFUNC PHP_FUNCTION(datefmt_get_timezone)
6969

7070
const TimeZone& tz = fetch_datefmt(dfo)->getTimeZone();
7171
TimeZone *tz_clone = tz.clone();
72-
if (tz_clone == NULL) {
72+
if (UNEXPECTED(tz_clone == NULL)) {
7373
intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
7474
"datefmt_get_timezone: Out of memory when cloning time zone",
7575
0);
@@ -142,7 +142,7 @@ U_CFUNC PHP_FUNCTION(datefmt_get_calendar_object)
142142
}
143143

144144
Calendar *cal_clone = cal->clone();
145-
if (cal_clone == NULL) {
145+
if (UNEXPECTED(cal_clone == NULL)) {
146146
intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
147147
"datefmt_get_calendar_object: Out of memory when cloning "
148148
"calendar", 0);
@@ -193,7 +193,7 @@ U_CFUNC PHP_FUNCTION(datefmt_set_calendar)
193193
if (cal_owned) {
194194
/* a non IntlCalendar was specified, we want to keep the timezone */
195195
TimeZone *old_timezone = fetch_datefmt(dfo)->getTimeZone().clone();
196-
if (old_timezone == NULL) {
196+
if (UNEXPECTED(old_timezone == NULL)) {
197197
intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
198198
"datefmt_set_calendar: Out of memory when cloning calendar",
199199
0);
@@ -203,7 +203,7 @@ U_CFUNC PHP_FUNCTION(datefmt_set_calendar)
203203
cal->adoptTimeZone(old_timezone);
204204
} else {
205205
cal = cal->clone();
206-
if (cal == NULL) {
206+
if (UNEXPECTED(cal == NULL)) {
207207
intl_errors_set(INTL_DATA_ERROR_P(dfo), U_MEMORY_ALLOCATION_ERROR,
208208
"datefmt_set_calendar: Out of memory when cloning calendar",
209209
0);

ext/intl/dateformat/dateformat_format_object.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ using icu::GregorianCalendar;
3737
using icu::StringPiece;
3838
using icu::SimpleDateFormat;
3939

40-
static const DateFormat::EStyle valid_styles[] = {
40+
static constexpr DateFormat::EStyle valid_styles[] = {
4141
DateFormat::kNone,
4242
DateFormat::kFull,
4343
DateFormat::kLong,

ext/intl/intl_convertcpp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern "C" {
2323
/* {{{ intl_stringFromChar */
2424
int intl_stringFromChar(UnicodeString &ret, char *str, size_t str_len, UErrorCode *status)
2525
{
26-
if(str_len > INT32_MAX) {
26+
if(UNEXPECTED(str_len > INT32_MAX)) {
2727
*status = U_BUFFER_OVERFLOW_ERROR;
2828
ret.setToBogus();
2929
return FAILURE;
@@ -56,7 +56,7 @@ zend_string* intl_charFromString(const UnicodeString &from, UErrorCode *status)
5656
{
5757
zend_string *u8res;
5858

59-
if (from.isBogus()) {
59+
if (UNEXPECTED(from.isBogus())) {
6060
return NULL;
6161
}
6262

ext/intl/msgformat/msgformat_helpers.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ static void umsg_set_timezone(MessageFormatter_object *mfo,
328328

329329
formats = mf->getFormats(count);
330330

331-
if (formats == NULL) {
331+
if (UNEXPECTED(formats == NULL)) {
332332
intl_errors_set(&err, U_MEMORY_ALLOCATION_ERROR,
333333
"Out of memory retrieving subformats", 0);
334334
}
@@ -403,7 +403,7 @@ U_CFUNC void umsg_format_helper(MessageFormatter_object *mfo,
403403
/* Process key and retrieve type */
404404
if (str_index == NULL) {
405405
/* includes case where index < 0 because it's exposed as unsigned */
406-
if (num_index > (zend_ulong)INT32_MAX) {
406+
if (UNEXPECTED(num_index > (zend_ulong)INT32_MAX)) {
407407
intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
408408
"Found negative or too large array key", 0);
409409
continue;
@@ -477,17 +477,17 @@ U_CFUNC void umsg_format_helper(MessageFormatter_object *mfo,
477477
int32_t tInt32 = 0;
478478

479479
if (Z_TYPE_P(elem) == IS_DOUBLE) {
480-
if (Z_DVAL_P(elem) > (double)INT32_MAX ||
481-
Z_DVAL_P(elem) < (double)INT32_MIN) {
480+
if (UNEXPECTED(Z_DVAL_P(elem) > (double)INT32_MAX ||
481+
Z_DVAL_P(elem) < (double)INT32_MIN)) {
482482
intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
483483
"Found PHP float with absolute value too large for "
484484
"32 bit integer argument", 0);
485485
} else {
486486
tInt32 = (int32_t)Z_DVAL_P(elem);
487487
}
488488
} else if (Z_TYPE_P(elem) == IS_LONG) {
489-
if (Z_LVAL_P(elem) > INT32_MAX ||
490-
Z_LVAL_P(elem) < INT32_MIN) {
489+
if (UNEXPECTED(Z_LVAL_P(elem) > INT32_MAX ||
490+
Z_LVAL_P(elem) < INT32_MIN)) {
491491
intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
492492
"Found PHP integer with absolute value too large "
493493
"for 32 bit integer argument", 0);
@@ -505,8 +505,8 @@ U_CFUNC void umsg_format_helper(MessageFormatter_object *mfo,
505505
int64_t tInt64 = 0;
506506

507507
if (Z_TYPE_P(elem) == IS_DOUBLE) {
508-
if (Z_DVAL_P(elem) > (double)U_INT64_MAX ||
509-
Z_DVAL_P(elem) < (double)U_INT64_MIN) {
508+
if (UNEXPECTED(Z_DVAL_P(elem) > (double)U_INT64_MAX ||
509+
Z_DVAL_P(elem) < (double)U_INT64_MIN)) {
510510
intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
511511
"Found PHP float with absolute value too large for "
512512
"64 bit integer argument", 0);

ext/intl/timezone/timezone_class.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone,
157157
return NULL;
158158
}
159159
timeZone = to->utimezone->clone();
160-
if (timeZone == NULL) {
160+
if (UNEXPECTED(timeZone == NULL)) {
161161
spprintf(&message, 0, "%s: could not clone TimeZone", func);
162162
if (message) {
163163
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,
193193
return NULL;
194194
}
195195
timeZone = TimeZone::createTimeZone(id);
196-
if (timeZone == NULL) {
196+
if (UNEXPECTED(timeZone == NULL)) {
197197
spprintf(&message, 0, "%s: Could not create time zone", func);
198198
if (message) {
199199
intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);

ext/intl/timezone/timezone_methods.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ U_CFUNC PHP_FUNCTION(intltz_create_enumeration)
148148
se = TimeZone::createEnumeration();
149149
} else if (Z_TYPE_P(arg) == IS_LONG) {
150150
int_offset:
151-
if (Z_LVAL_P(arg) < (zend_long)INT32_MIN ||
152-
Z_LVAL_P(arg) > (zend_long)INT32_MAX) {
151+
if (UNEXPECTED(Z_LVAL_P(arg) < (zend_long)INT32_MIN ||
152+
Z_LVAL_P(arg) > (zend_long)INT32_MAX)) {
153153
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
154154
"intltz_create_enumeration: value is out of range", 0);
155155
RETURN_FALSE;
@@ -241,7 +241,7 @@ U_CFUNC PHP_FUNCTION(intltz_create_time_zone_id_enumeration)
241241
}
242242

243243
if (!arg3isnull) {
244-
if (offset_arg < (zend_long)INT32_MIN || offset_arg > (zend_long)INT32_MAX) {
244+
if (UNEXPECTED(offset_arg < (zend_long)INT32_MIN || offset_arg > (zend_long)INT32_MAX)) {
245245
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
246246
"intltz_create_time_zone_id_enumeration: offset out of bounds", 0);
247247
RETURN_FALSE;
@@ -350,7 +350,7 @@ U_CFUNC PHP_FUNCTION(intltz_get_equivalent_id)
350350
RETURN_THROWS();
351351
}
352352

353-
if (index < (zend_long)INT32_MIN || index > (zend_long)INT32_MAX) {
353+
if (UNEXPECTED(index < (zend_long)INT32_MIN || index > (zend_long)INT32_MAX)) {
354354
RETURN_FALSE;
355355
}
356356

@@ -475,7 +475,7 @@ U_CFUNC PHP_FUNCTION(intltz_has_same_rules)
475475
RETURN_BOOL(to->utimezone->hasSameRules(*other_to->utimezone));
476476
}
477477

478-
static const TimeZone::EDisplayType display_types[] = {
478+
static constexpr TimeZone::EDisplayType display_types[] = {
479479
TimeZone::SHORT, TimeZone::LONG,
480480
TimeZone::SHORT_GENERIC, TimeZone::LONG_GENERIC,
481481
TimeZone::SHORT_GMT, TimeZone::LONG_GMT,

0 commit comments

Comments
 (0)