Skip to content

Commit 5abf853

Browse files
committed
ext/intl: idn.c use ValueErrors where appropriate
Drive-by refactoring
1 parent c29eed4 commit 5abf853

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

ext/intl/idn/idn.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ enum {
3737
};
3838

3939
/* like INTL_CHECK_STATUS, but as a function and varying the name of the func */
40-
static int php_intl_idn_check_status(UErrorCode err, const char *msg)
40+
static zend_result php_intl_idn_check_status(UErrorCode err, const char *msg)
4141
{
4242
intl_error_set_code(NULL, err);
4343
if (U_FAILURE(err)) {
@@ -53,11 +53,6 @@ static int php_intl_idn_check_status(UErrorCode err, const char *msg)
5353
return SUCCESS;
5454
}
5555

56-
static inline void php_intl_bad_args(const char *msg)
57-
{
58-
php_intl_idn_check_status(U_ILLEGAL_ARGUMENT_ERROR, msg);
59-
}
60-
6156
static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS,
6257
const zend_string *domain, uint32_t option, int mode, zval *idna_info)
6358
{
@@ -128,18 +123,17 @@ static void php_intl_idn_handoff(INTERNAL_FUNCTION_PARAMETERS, int mode)
128123
RETURN_THROWS();
129124
}
130125

131-
if (variant != INTL_IDN_VARIANT_UTS46) {
132-
php_intl_bad_args("invalid variant, must be INTL_IDNA_VARIANT_UTS46");
133-
RETURN_FALSE;
134-
}
135-
136-
if (ZSTR_LEN(domain) < 1) {
137-
php_intl_bad_args("empty domain name");
138-
RETURN_FALSE;
126+
if (ZSTR_LEN(domain) == 0) {
127+
zend_argument_value_error(1, "cannot be empty");
128+
RETURN_THROWS();
139129
}
140130
if (ZSTR_LEN(domain) > INT32_MAX - 1) {
141-
php_intl_bad_args("domain name too large");
142-
RETURN_FALSE;
131+
zend_argument_value_error(1, "must be less than " PRId32 " bytes", INT32_MAX);
132+
RETURN_THROWS();
133+
}
134+
if (variant != INTL_IDN_VARIANT_UTS46) {
135+
zend_argument_value_error(2, "must be INTL_IDNA_VARIANT_UTS46");
136+
RETURN_THROWS();
143137
}
144138
/* don't check options; it wasn't checked before */
145139

ext/intl/tests/idn_uts46_errors.phpt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ ini_set("intl.error_level", E_WARNING);
1313
echo "=> PHP level errors", "\n";
1414

1515
echo "bad variant:", "\n";
16-
var_dump(idn_to_ascii("", 0, INTL_IDNA_VARIANT_UTS46 + 10));
16+
try {
17+
var_dump(idn_to_ascii("domain", 0, INTL_IDNA_VARIANT_UTS46 + 10));
18+
} catch (Throwable $e) {
19+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
20+
}
1721

1822
echo "empty domain:", "\n";
19-
var_dump(idn_to_ascii("", 0, INTL_IDNA_VARIANT_UTS46));
23+
try {
24+
var_dump(idn_to_ascii("", 0, INTL_IDNA_VARIANT_UTS46));
25+
} catch (Throwable $e) {
26+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
27+
}
2028

2129
echo "with error, but no details arg:", "\n";
2230
var_dump(idn_to_ascii("www.fußball.com-", 0, INTL_IDNA_VARIANT_UTS46));
@@ -26,7 +34,7 @@ var_dump(idn_to_ascii("www.fußball.com-", IDNA_NONTRANSITIONAL_TO_ASCII,
2634
INTL_IDNA_VARIANT_UTS46, $foo));
2735
var_dump($foo);
2836

29-
echo "with error, with details arg, contextj:", "\n";
37+
echo "with error, with details arg, context:", "\n";
3038
var_dump(idn_to_ascii(
3139
html_entity_decode("www.a&#x200D;b.com", 0, "UTF-8"),
3240
IDNA_NONTRANSITIONAL_TO_ASCII | IDNA_CHECK_CONTEXTJ,
@@ -35,16 +43,12 @@ var_dump($foo);
3543
var_dump($foo["errors"]==IDNA_ERROR_CONTEXTJ);
3644

3745
?>
38-
--EXPECTF--
46+
--EXPECT--
3947
=> PHP level errors
4048
bad variant:
41-
42-
Warning: idn_to_ascii(): idn_to_ascii: invalid variant, must be INTL_IDNA_VARIANT_UTS46 in %s on line %d
43-
bool(false)
49+
ValueError: idn_to_ascii(): Argument #2 ($flags) must be INTL_IDNA_VARIANT_UTS46
4450
empty domain:
45-
46-
Warning: idn_to_ascii(): idn_to_ascii: empty domain name in %s on line %d
47-
bool(false)
51+
ValueError: idn_to_ascii(): Argument #1 ($domain) cannot be empty
4852
with error, but no details arg:
4953
bool(false)
5054
with error, with details arg:
@@ -57,7 +61,7 @@ array(3) {
5761
["errors"]=>
5862
int(16)
5963
}
60-
with error, with details arg, contextj:
64+
with error, with details arg, context:
6165
bool(false)
6266
array(3) {
6367
["result"]=>

0 commit comments

Comments
 (0)