Skip to content

Commit d9ffb18

Browse files
committed
changes from remarks
1 parent 85675f4 commit d9ffb18

5 files changed

+99
-43
lines changed

ext/intl/tests/timezone_createEnumeration_error.phpt

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,35 @@ try {
1111
} catch (TypeError $e) {
1212
echo $e->getMessage() . PHP_EOL;
1313
}
14+
15+
try {
16+
IntlTimeZone::createEnumeration(str_repeat("8192", 1024));
17+
} catch (ValueError $e) {
18+
echo $e->getMessage() . PHP_EOL;
19+
}
20+
21+
try {
22+
IntlTimeZone::createEnumeration(31.9998157);
23+
} catch (ValueError $e) {
24+
echo $e->getMessage() . PHP_EOL;
25+
}
26+
27+
try {
28+
IntlTimeZone::createEnumeration(-2147483649);
29+
} catch (ValueError $e) {
30+
echo $e->getMessage() . PHP_EOL;
31+
}
32+
try {
33+
IntlTimeZone::createEnumeration(2147483648);
34+
} catch (ValueError $e) {
35+
echo $e->getMessage() . PHP_EOL;
36+
}
1437
?>
15-
--EXPECT--
16-
IntlTimeZone::createEnumeration(): Argument #1 ($countryOrRawOffset) invalid argument type
38+
--EXPECTF--
39+
IntlTimeZone::createEnumeration(): Argument #1 ($countryOrRawOffset) must be of type string|int, array given
40+
41+
Deprecated: Implicit conversion from float INF to int loses precision in %s on line %d
42+
43+
Deprecated: Implicit conversion from float %f to int loses precision in %s on line %d
44+
IntlTimeZone::createEnumeration(): Argument #1 ($countryOrRawOffset) value must be between %s and %s
45+
IntlTimeZone::createEnumeration(): Argument #1 ($countryOrRawOffset) value must be between %s and %s

ext/intl/tests/timezone_createTimeZoneIDEnumeration_error.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ intl
77
ini_set("intl.error_level", E_WARNING);
88

99
var_dump(IntlTimeZone::createTimeZoneIDEnumeration(-1));
10+
11+
try {
12+
IntlTimeZone::createTimeZoneIDEnumeration(IntlTimeZone::TYPE_ANY, null, -2147483649);
13+
} catch (ValueError $e) {
14+
echo $e->getMessage() . PHP_EOL;
15+
}
16+
try {
17+
IntlTimeZone::createTimeZoneIDEnumeration(IntlTimeZone::TYPE_ANY, null, 2147483648);
18+
} catch (ValueError $e) {
19+
echo $e->getMessage();
20+
}
1021
?>
1122
--EXPECTF--
1223
Warning: IntlTimeZone::createTimeZoneIDEnumeration(): bad zone type in %s on line %d
1324
bool(false)
25+
IntlTimeZone::createTimeZoneIDEnumeration(): Argument #3 ($rawOffset) offset must be between %s and %s
26+
IntlTimeZone::createTimeZoneIDEnumeration(): Argument #3 ($rawOffset) offset must be between %s and %s

ext/intl/tests/timezone_fromDateTimeZone_error.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ intl
88
<?php
99
ini_set("intl.error_level", E_WARNING);
1010

11+
$r = new ReflectionClass("DateTimeZone");
12+
try {
13+
IntlTimeZone::fromDateTimeZone($r->newInstanceWithoutConstructor());
14+
} catch (\Error $e) {
15+
echo $e->getMessage(). PHP_EOL;
16+
}
17+
1118
$dt = new DateTime('2012-08-01 00:00:00 WEST');
1219
var_dump(IntlTimeZone::fromDateTimeZone($dt->getTimeZone()));
1320
?>
1421
--EXPECTF--
22+
DateTimeZone object is unconstructed
23+
1524
Warning: IntlTimeZone::fromDateTimeZone(): intltz_from_date_time_zone: time zone id 'WEST' extracted from ext/date DateTimeZone not recognized in %s on line %d
1625
NULL

ext/intl/tests/timezone_getEquivalentID_error.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@ intl
77
ini_set("intl.error_level", E_WARNING);
88

99
var_dump(IntlTimeZone::getEquivalentID("foo\x80", 0));
10+
11+
try {
12+
IntlTimeZOne::getEquivalentID("NL", -2147483649);
13+
} catch (ValueError $e) {
14+
echo $e->getMessage() . PHP_EOL;
15+
}
16+
17+
try {
18+
IntlTimeZOne::getEquivalentID("NL", 2147483648);
19+
} catch (ValueError $e) {
20+
echo $e->getMessage();
21+
}
1022
?>
1123
--EXPECTF--
1224
Warning: IntlTimeZone::getEquivalentID(): could not convert time zone id to UTF-16 in %s on line %d
1325
bool(false)
26+
IntlTimeZone::getEquivalentID(): Argument #2 ($offset) index must be between %s and %s
27+
IntlTimeZone::getEquivalentID(): Argument #2 ($offset) index must be between %s and %s

ext/intl/timezone/timezone_methods.cpp

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -133,53 +133,44 @@ U_CFUNC PHP_FUNCTION(intltz_get_unknown)
133133
U_CFUNC PHP_FUNCTION(intltz_create_enumeration)
134134
{
135135
zval *arg = NULL;
136-
StringEnumeration *se = NULL;
136+
zend_string *args = NULL;
137+
zend_long argl = 0;
138+
StringEnumeration *se = NULL;
137139
intl_error_reset(NULL);
138140

139-
/* double indirection to have the zend engine destroy the new zval that
140-
* results from separation */
141-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
142-
RETURN_THROWS();
143-
}
141+
ZEND_PARSE_PARAMETERS_START(0, 1)
142+
Z_PARAM_OPTIONAL
143+
Z_PARAM_STR_OR_LONG(args, argl);
144+
ZEND_PARSE_PARAMETERS_END();
144145

145-
if (arg == NULL || Z_TYPE_P(arg) == IS_NULL) {
146+
if (ZEND_NUM_ARGS() == 0) {
146147
se = TimeZone::createEnumeration();
147-
} else if (Z_TYPE_P(arg) == IS_LONG) {
148-
int_offset:
149-
if (UNEXPECTED(Z_LVAL_P(arg) < (zend_long)INT32_MIN ||
150-
Z_LVAL_P(arg) > (zend_long)INT32_MAX)) {
151-
zend_argument_value_error(1, "value is out of range");
152-
RETURN_THROWS();
148+
} else {
149+
if (args) {
150+
double dval;
151+
switch (is_numeric_string(ZSTR_VAL(args), ZSTR_LEN(args), &argl, &dval, 0)) {
152+
case IS_DOUBLE:
153+
argl = zend_dval_to_lval(dval);
154+
if (!zend_is_long_compatible(argl, dval)) {
155+
zend_incompatible_double_to_long_error(dval);
156+
}
157+
case IS_LONG:
158+
goto long_value;
159+
default:
160+
se = TimeZone::createEnumeration(ZSTR_VAL(args));
161+
}
153162
} else {
154-
se = TimeZone::createEnumeration((int32_t) Z_LVAL_P(arg));
155-
}
156-
} else if (Z_TYPE_P(arg) == IS_DOUBLE) {
157-
double_offset:
158-
convert_to_long(arg);
159-
goto int_offset;
160-
} else if (Z_TYPE_P(arg) == IS_OBJECT || Z_TYPE_P(arg) == IS_STRING) {
161-
zend_long lval;
162-
double dval;
163-
if (!try_convert_to_string(arg)) {
164-
RETURN_THROWS();
165-
}
166-
switch (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), &lval, &dval, 0)) {
167-
case IS_DOUBLE:
168-
zval_ptr_dtor(arg);
169-
ZVAL_DOUBLE(arg, dval);
170-
goto double_offset;
171-
case IS_LONG:
172-
zval_ptr_dtor(arg);
173-
ZVAL_LONG(arg, lval);
174-
goto int_offset;
163+
long_value:
164+
if (UNEXPECTED(argl < (zend_long)INT32_MIN || argl > (zend_long)INT32_MAX)) {
165+
zend_argument_value_error(1, "value must be between %d and %d", INT32_MIN, INT32_MAX);
166+
RETURN_THROWS();
167+
} else {
168+
se = TimeZone::createEnumeration((int32_t) argl);
169+
}
175170
}
176-
/* else call string version */
177-
se = TimeZone::createEnumeration(Z_STRVAL_P(arg));
178-
} else {
179-
zend_argument_type_error(1, "invalid argument type");
180-
RETURN_THROWS();
181171
}
182172

173+
183174
if (se) {
184175
IntlIterator_from_StringEnumeration(se, return_value);
185176
} else {
@@ -238,7 +229,7 @@ U_CFUNC PHP_FUNCTION(intltz_create_time_zone_id_enumeration)
238229

239230
if (!arg3isnull) {
240231
if (UNEXPECTED(offset_arg < (zend_long)INT32_MIN || offset_arg > (zend_long)INT32_MAX)) {
241-
zend_argument_value_error(3, "offset out of bounds");
232+
zend_argument_value_error(3, "offset must be between %d and %d", INT32_MIN, INT32_MAX);
242233
RETURN_THROWS();
243234
}
244235
offset = (int32_t)offset_arg;
@@ -344,7 +335,7 @@ U_CFUNC PHP_FUNCTION(intltz_get_equivalent_id)
344335
}
345336

346337
if (UNEXPECTED(index < (zend_long)INT32_MIN || index > (zend_long)INT32_MAX)) {
347-
zend_argument_value_error(2, "index out of range");
338+
zend_argument_value_error(2, "index must be between %d and %d", INT32_MIN, INT32_MAX);
348339
RETURN_THROWS();
349340
}
350341

0 commit comments

Comments
 (0)