Skip to content

Commit fd0b399

Browse files
committed
Promote warnings to exceptions in ext/intl
Closes GH-5972
1 parent 174dadf commit fd0b399

18 files changed

+272
-146
lines changed

ext/intl/converter/converter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,8 @@ PHP_METHOD(UConverter, reasonText) {
693693
UCNV_REASON_CASE(CLOSE)
694694
UCNV_REASON_CASE(CLONE)
695695
default:
696-
php_error_docref(NULL, E_WARNING, "Unknown UConverterCallbackReason: " ZEND_LONG_FMT, reason);
697-
RETURN_FALSE;
696+
zend_argument_value_error(1, "must be a UConverter::REASON_* constant");
697+
RETURN_THROWS();
698698
}
699699
}
700700
/* }}} */

ext/intl/converter/converter.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static function getStandards() {}
4545
/** @return string|false|null */
4646
public function getSubstChars() {}
4747

48-
/** @return string|false */
48+
/** @return string */
4949
public static function reasonText(int $reason) {}
5050

5151
/** @return bool */

ext/intl/converter/converter_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 9eef3fe293c07ab77f4c8b6d8d53a3798f8a9865 */
2+
* Stub hash: e33e2614c969c59b79c6062f7a347a8e8e486d85 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_UConverter___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, destination_encoding, IS_STRING, 1, "null")

ext/intl/formatter/formatter_format.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ PHP_FUNCTION( numfmt_format )
105105
break;
106106

107107
default:
108-
php_error_docref(NULL, E_WARNING, "Unsupported format type " ZEND_LONG_FMT, type);
109-
RETURN_FALSE;
110-
break;
108+
zend_argument_value_error(3, "must be a NumberFormatter::TYPE_* constant");
109+
RETURN_THROWS();
111110
}
112111

113112
INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );

ext/intl/formatter/formatter_parse.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ PHP_FUNCTION( numfmt_parse )
4444
FORMATTER_METHOD_INIT_VARS;
4545

4646
/* Parse parameters. */
47-
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|lz!",
47+
if (zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|lz!",
4848
&object, NumberFormatter_ce_ptr, &str, &str_len, &type, &zposition ) == FAILURE )
4949
{
5050
RETURN_THROWS();
5151
}
5252

53-
if(zposition) {
53+
if (zposition) {
5454
position = (int32_t) zval_get_long(zposition);
5555
position_p = &position;
5656
}
@@ -86,17 +86,20 @@ PHP_FUNCTION( numfmt_parse )
8686
RETVAL_DOUBLE(val_double);
8787
break;
8888
default:
89-
php_error_docref(NULL, E_WARNING, "Unsupported format type " ZEND_LONG_FMT, type);
90-
RETVAL_FALSE;
91-
break;
89+
zend_argument_value_error(3, "must be a NumberFormatter::TYPE_* constant");
90+
goto cleanup;
91+
}
92+
93+
if (zposition) {
94+
ZEND_TRY_ASSIGN_REF_LONG(zposition, position);
9295
}
96+
97+
cleanup:
98+
9399
#if ICU_LOCALE_BUG && defined(LC_NUMERIC)
94100
setlocale(LC_NUMERIC, oldlocale);
95101
efree(oldlocale);
96102
#endif
97-
if(zposition) {
98-
ZEND_TRY_ASSIGN_REF_LONG(zposition, position);
99-
}
100103

101104
if (sstr) {
102105
efree(sstr);

ext/intl/grapheme/grapheme_string.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ PHP_FUNCTION(grapheme_strpos)
114114
}
115115

116116
if ( OUTSIDE_STRING(loffset, haystack_len) ) {
117-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 );
118-
RETURN_FALSE;
117+
zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
118+
RETURN_THROWS();
119119
}
120120

121121
/* we checked that it will fit: */
@@ -125,8 +125,8 @@ PHP_FUNCTION(grapheme_strpos)
125125
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
126126

127127
if (needle_len == 0) {
128-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 );
129-
RETURN_FALSE;
128+
zend_argument_value_error(2, "cannot be empty");
129+
RETURN_THROWS();
130130
}
131131

132132
if (offset >= 0) {
@@ -154,7 +154,6 @@ PHP_FUNCTION(grapheme_strpos)
154154
} else {
155155
RETURN_FALSE;
156156
}
157-
158157
}
159158
/* }}} */
160159

@@ -174,8 +173,8 @@ PHP_FUNCTION(grapheme_stripos)
174173
}
175174

176175
if ( OUTSIDE_STRING(loffset, haystack_len) ) {
177-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: Offset not contained in string", 1 );
178-
RETURN_FALSE;
176+
zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
177+
RETURN_THROWS();
179178
}
180179

181180
/* we checked that it will fit: */
@@ -184,8 +183,8 @@ PHP_FUNCTION(grapheme_stripos)
184183
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
185184

186185
if (needle_len == 0) {
187-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: Empty delimiter", 1 );
188-
RETURN_FALSE;
186+
zend_argument_value_error(2, "cannot be empty");
187+
RETURN_THROWS();
189188
}
190189

191190
is_ascii = ( grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0 );
@@ -240,8 +239,8 @@ PHP_FUNCTION(grapheme_strrpos)
240239
}
241240

242241
if ( OUTSIDE_STRING(loffset, haystack_len) ) {
243-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 );
244-
RETURN_FALSE;
242+
zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
243+
RETURN_THROWS();
245244
}
246245

247246
/* we checked that it will fit: */
@@ -250,8 +249,8 @@ PHP_FUNCTION(grapheme_strrpos)
250249
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
251250

252251
if (needle_len == 0) {
253-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 );
254-
RETURN_FALSE;
252+
zend_argument_value_error(2, "cannot be empty");
253+
RETURN_THROWS();
255254
}
256255

257256
is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0;
@@ -300,8 +299,8 @@ PHP_FUNCTION(grapheme_strripos)
300299
}
301300

302301
if ( OUTSIDE_STRING(loffset, haystack_len) ) {
303-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 );
304-
RETURN_FALSE;
302+
zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
303+
RETURN_THROWS();
305304
}
306305

307306
/* we checked that it will fit: */
@@ -310,8 +309,8 @@ PHP_FUNCTION(grapheme_strripos)
310309
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
311310

312311
if (needle_len == 0) {
313-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 );
314-
RETURN_FALSE;
312+
zend_argument_value_error(2, "cannot be empty");
313+
RETURN_THROWS();
315314
}
316315

317316
is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0;
@@ -377,8 +376,8 @@ PHP_FUNCTION(grapheme_substr)
377376
}
378377

379378
if ( OUTSIDE_STRING(lstart, str_len)) {
380-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: start not contained in string", 1 );
381-
RETURN_FALSE;
379+
zend_argument_value_error(2, "must be contained in argument #1 ($string)");
380+
RETURN_THROWS();
382381
}
383382

384383
/* we checked that it will fit: */
@@ -532,10 +531,9 @@ PHP_FUNCTION(grapheme_substr)
532531

533532
if ( UBRK_DONE == sub_str_end_pos) {
534533
if(length < 0) {
535-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: length not contained in string", 1 );
536-
534+
zend_argument_value_error(3, "must be contained in argument #1 ($string)");
537535
efree(ustr);
538-
RETURN_FALSE;
536+
RETURN_THROWS();
539537
} else {
540538
sub_str_end_pos = ustr_len;
541539
}
@@ -582,13 +580,10 @@ static void strstr_common_handler(INTERNAL_FUNCTION_PARAMETERS, int f_ignore_cas
582580
}
583581

584582
if (needle_len == 0) {
585-
586-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 );
587-
588-
RETURN_FALSE;
583+
zend_argument_value_error(2, "cannot be empty");
584+
RETURN_THROWS();
589585
}
590586

591-
592587
if ( !f_ignore_case ) {
593588

594589
/* ASCII optimization: quick check to see if the string might be there
@@ -783,20 +778,25 @@ PHP_FUNCTION(grapheme_extract)
783778
}
784779

785780
if ( extract_type < GRAPHEME_EXTRACT_TYPE_MIN || extract_type > GRAPHEME_EXTRACT_TYPE_MAX ) {
786-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
787-
"grapheme_extract: unknown extract type param", 0 );
788-
RETURN_FALSE;
781+
zend_argument_value_error(3, "must be either GRAPHEME_EXTR_COUNT, GRAPHEME_EXTR_MAXBYTES, or GRAPHEME_EXTR_MAXCHARS");
782+
RETURN_THROWS();
789783
}
790784

791785
if ( lstart > INT32_MAX || lstart < 0 || (size_t)lstart >= str_len ) {
792786
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: start not contained in string", 0 );
793787
RETURN_FALSE;
794788
}
795789

796-
if ( size > INT32_MAX || size < 0) {
797-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: size is invalid", 0 );
798-
RETURN_FALSE;
790+
if (size < 0) {
791+
zend_argument_value_error(2, "must be greater than or equal to 0");
792+
RETURN_THROWS();
799793
}
794+
795+
if (size > INT32_MAX) {
796+
zend_argument_value_error(2, "is too large");
797+
RETURN_THROWS();
798+
}
799+
800800
if (size == 0) {
801801
RETURN_EMPTY_STRING();
802802
}

ext/intl/locale/locale_methods.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,9 @@ PHP_FUNCTION(locale_compose)
882882
/* Not grandfathered */
883883
result = append_key_value(loc_name, hash_arr , LOC_LANG_TAG);
884884
if( result == LOC_NOT_FOUND ){
885-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
886-
"locale_compose: parameter array does not contain 'language' tag.", 0 );
885+
zend_argument_value_error(1, "must contain a \"%s\" key", LOC_LANG_TAG);
887886
smart_str_free(loc_name);
888-
RETURN_FALSE;
887+
RETURN_THROWS();
889888
}
890889
if( !handleAppendResult( result, loc_name)){
891890
RETURN_FALSE;
@@ -1348,7 +1347,7 @@ static zend_string* lookup_loc_range(const char* loc_range, HashTable* hash_arr,
13481347
/* convert the array to lowercase , also replace hyphens with the underscore and store it in cur_arr */
13491348
if(Z_TYPE_P(ele_value)!= IS_STRING) {
13501349
/* element value is not a string */
1351-
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "lookup_loc_range: locale array element is not a string", 0);
1350+
zend_argument_type_error(2, "must only contain string values");
13521351
LOOKUP_CLEAN_RETURN(NULL);
13531352
}
13541353
cur_arr[cur_arr_len*2] = estrndup(Z_STRVAL_P(ele_value), Z_STRLEN_P(ele_value));

ext/intl/normalizer/normalizer_normalize.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ PHP_FUNCTION( normalizer_normalize )
121121
#endif
122122
break;
123123
default:
124-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
125-
"normalizer_normalize: illegal normalization form", 0 );
126-
RETURN_FALSE;
124+
zend_argument_value_error(2, "must be a a valid normalization form");
125+
RETURN_THROWS();
127126
}
128127

129128
/*
@@ -248,9 +247,8 @@ PHP_FUNCTION( normalizer_is_normalized )
248247
#endif
249248
break;
250249
default:
251-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
252-
"normalizer_normalize: illegal normalization form", 0 );
253-
RETURN_FALSE;
250+
zend_argument_value_error(2, "must be a a valid normalization form");
251+
RETURN_THROWS();
254252
}
255253

256254

ext/intl/resourcebundle/resourcebundle_class.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ static int resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS)
105105
}
106106

107107
if (bundlename_len >= MAXPATHLEN) {
108-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "Bundle name too long", 0 );
109-
zval_ptr_dtor(return_value);
110-
ZVAL_NULL(return_value);
108+
zend_argument_value_error(2, "is too long");
111109
return FAILURE;
112110
}
113111

@@ -296,8 +294,8 @@ PHP_FUNCTION( resourcebundle_locales )
296294
}
297295

298296
if (bundlename_len >= MAXPATHLEN) {
299-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "resourcebundle_locales: bundle name too long", 0 );
300-
RETURN_FALSE;
297+
zend_argument_value_error(1, "is too long");
298+
RETURN_THROWS();
301299
}
302300

303301
if(bundlename_len == 0) {

ext/intl/tests/bug61487.phpt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ if (PHP_INT_SIZE != 8) die('skip 64-bit only');
66
?>
77
--FILE--
88
<?php
9-
var_dump(grapheme_stripos(1,1,2147483648));
10-
var_dump(grapheme_strpos(1,1,2147483648));
9+
try {
10+
grapheme_stripos(1,1,2147483648);
11+
} catch (ValueError $exception) {
12+
echo $exception->getMessage() . "\n";
13+
}
14+
15+
try {
16+
grapheme_strpos(1,1,2147483648);
17+
} catch (ValueError $exception) {
18+
echo $exception->getMessage() . "\n";
19+
}
1120
?>
1221
--EXPECT--
13-
bool(false)
14-
bool(false)
22+
grapheme_stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
23+
grapheme_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)

ext/intl/tests/bug62083.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ if (!extension_loaded('intl'))
77
--FILE--
88
<?php
99
$arr1 = array();
10-
var_dump(grapheme_extract(-1, -1, -1,-1, $arr1));
10+
try {
11+
grapheme_extract(-1, -1, -1,-1, $arr1);
12+
} catch (ValueError $exception) {
13+
echo $exception->getMessage() . "\n";
14+
}
1115
?>
1216
--EXPECT--
13-
bool(false)
17+
grapheme_extract(): Argument #3 ($extract_type) must be either GRAPHEME_EXTR_COUNT, GRAPHEME_EXTR_MAXBYTES, or GRAPHEME_EXTR_MAXCHARS

0 commit comments

Comments
 (0)