From: George Peter Banyard Date: Fri, 3 Apr 2020 17:14:42 +0000 (+0200) Subject: Promote some warnings to ValueError in mbstring X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=07062e1fc58b09e459eff2265b89985124e3ada8;p=php Promote some warnings to ValueError in mbstring Promoted warnings are: * Empty encoding lists * Unknown language * Start and Width out of bound --- diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index c2c7954af9..454440ef6e 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -1365,11 +1365,12 @@ PHP_FUNCTION(mb_language) } else { zend_string *ini_name = zend_string_init("mbstring.language", sizeof("mbstring.language") - 1, 0); if (FAILURE == zend_alter_ini_entry(ini_name, name, PHP_INI_USER, PHP_INI_STAGE_RUNTIME)) { - php_error_docref(NULL, E_WARNING, "Unknown language \"%s\"", ZSTR_VAL(name)); - RETVAL_FALSE; - } else { - RETVAL_TRUE; + zend_argument_value_error(1, "must be a valid language, \"%s\" given", ZSTR_VAL(name)); + zend_string_release_ex(ini_name, 0); + RETURN_THROWS(); } + // TODO Make return void + RETVAL_TRUE; zend_string_release_ex(ini_name, 0); } } @@ -1568,8 +1569,8 @@ PHP_FUNCTION(mb_detect_order) if (size == 0) { efree(list); - php_error_docref(NULL, E_WARNING, "Must specify at least one encoding"); - RETURN_FALSE; + zend_argument_value_error(1, "must specify at least one encoding"); + RETURN_THROWS(); } if (MBSTRG(current_detect_order_list)) { @@ -2481,8 +2482,8 @@ PHP_FUNCTION(mb_strimwidth) } if (from < 0 || (size_t)from > str_len) { - php_error_docref(NULL, E_WARNING, "Start position is out of range"); - RETURN_FALSE; + zend_argument_value_error(2, "is out of range"); + RETURN_THROWS(); } if (width < 0) { @@ -2490,8 +2491,8 @@ PHP_FUNCTION(mb_strimwidth) } if (width < 0) { - php_error_docref(NULL, E_WARNING, "Width is out of range"); - RETURN_FALSE; + zend_argument_value_error(3, "is out of range"); + RETURN_THROWS(); } if (trimmarker) { @@ -2721,8 +2722,8 @@ PHP_FUNCTION(mb_convert_encoding) if (!num_from_encodings) { efree(from_encodings); - php_error_docref(NULL, E_WARNING, "Must specify at least one encoding"); - RETURN_FALSE; + zend_argument_value_error(3, "must specify at least one encoding"); + RETURN_THROWS(); } if (input_str) { @@ -2907,8 +2908,8 @@ PHP_FUNCTION(mb_detect_encoding) if (size == 0) { efree(elist); - php_error_docref(NULL, E_WARNING, "Must specify at least one encoding"); - RETURN_FALSE; + zend_argument_value_error(2, "must specify at least one encoding"); + RETURN_THROWS(); } if (ZEND_NUM_ARGS() < 3) { @@ -3301,8 +3302,8 @@ PHP_FUNCTION(mb_convert_variables) if (elistsz == 0) { efree(elist); - php_error_docref(NULL, E_WARNING, "Must specify at least one encoding"); - RETURN_FALSE; + zend_argument_value_error(2, "must specify at least one encoding"); + RETURN_THROWS(); } if (elistsz == 1) { diff --git a/ext/mbstring/tests/mb_convert_encoding_empty_encoding_list.phpt b/ext/mbstring/tests/mb_convert_encoding_empty_encoding_list.phpt new file mode 100644 index 0000000000..7b7d8217c3 --- /dev/null +++ b/ext/mbstring/tests/mb_convert_encoding_empty_encoding_list.phpt @@ -0,0 +1,27 @@ +--TEST-- +Test mb_convert_encoding() function : empty encoding list +--SKIPIF-- + +--FILE-- +getMessage() . \PHP_EOL; +} +try { + var_dump( mb_convert_encoding($string, 'UTF-8', [])); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} + +?> +--EXPECT-- +mb_convert_encoding(): Argument #3 ($from) must specify at least one encoding +mb_convert_encoding(): Argument #3 ($from) must specify at least one encoding diff --git a/ext/mbstring/tests/mb_convert_variables_empty_encoding_list.phpt b/ext/mbstring/tests/mb_convert_variables_empty_encoding_list.phpt new file mode 100644 index 0000000000..4dbbd9f26f --- /dev/null +++ b/ext/mbstring/tests/mb_convert_variables_empty_encoding_list.phpt @@ -0,0 +1,26 @@ +--TEST-- +Test mb_convert_variables() function : empty encoding list +--SKIPIF-- + +--FILE-- +getMessage() . \PHP_EOL; +} +try { + var_dump( mb_convert_variables('UTF-8', [], $string)); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} + +?> +--EXPECT-- +mb_convert_variables(): Argument #2 ($from) must specify at least one encoding +mb_convert_variables(): Argument #2 ($from) must specify at least one encoding diff --git a/ext/mbstring/tests/mb_detect_encoding_empty_encoding_list.phpt b/ext/mbstring/tests/mb_detect_encoding_empty_encoding_list.phpt new file mode 100644 index 0000000000..28dbc5baa6 --- /dev/null +++ b/ext/mbstring/tests/mb_detect_encoding_empty_encoding_list.phpt @@ -0,0 +1,26 @@ +--TEST-- +Test mb_detect_encoding() function : empty encoding list +--SKIPIF-- + +--FILE-- +getMessage() . \PHP_EOL; +} +try { + var_dump( mb_detect_encoding($string, [])); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} + +?> +--EXPECT-- +mb_detect_encoding(): Argument #2 ($encoding_list) must specify at least one encoding +mb_detect_encoding(): Argument #2 ($encoding_list) must specify at least one encoding diff --git a/ext/mbstring/tests/mb_detect_order_empty_encoding_list.phpt b/ext/mbstring/tests/mb_detect_order_empty_encoding_list.phpt new file mode 100644 index 0000000000..7305bdf22f --- /dev/null +++ b/ext/mbstring/tests/mb_detect_order_empty_encoding_list.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test mb_detect_order() function : empty encoding list +--SKIPIF-- + +--FILE-- +getMessage() . \PHP_EOL; +} +try { + var_dump( mb_detect_order([])); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} + +?> +--EXPECT-- +mb_detect_order(): Argument #1 ($encoding) must specify at least one encoding +mb_detect_order(): Argument #1 ($encoding) must specify at least one encoding diff --git a/ext/mbstring/tests/mb_language.phpt b/ext/mbstring/tests/mb_language.phpt index 0b8bcff28c..ef7b4c260f 100644 --- a/ext/mbstring/tests/mb_language.phpt +++ b/ext/mbstring/tests/mb_language.phpt @@ -20,10 +20,14 @@ echo "Confirm language was changed:\n"; var_dump(mb_language()); echo "Try changing to a non-existent language:\n"; -var_dump(mb_language('Pig Latin')); +try { + var_dump(mb_language('Pig Latin')); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} var_dump(mb_language()); ?> ---EXPECTF-- +--EXPECT-- Checking default language: string(7) "neutral" Checking default language after ini_set: @@ -33,7 +37,5 @@ bool(true) Confirm language was changed: string(7) "English" Try changing to a non-existent language: - -Warning: mb_language(): Unknown language "Pig Latin" in %s on line %d -bool(false) +mb_language(): Argument #1 ($language) must be a valid language, "Pig Latin" given string(7) "neutral" diff --git a/ext/mbstring/tests/mb_strimwidth.phpt b/ext/mbstring/tests/mb_strimwidth.phpt index f5d516c2c4..ad346ef038 100644 --- a/ext/mbstring/tests/mb_strimwidth.phpt +++ b/ext/mbstring/tests/mb_strimwidth.phpt @@ -21,20 +21,29 @@ print "5: ". mb_strimwidth($euc_jp, 38, 5,'...','EUC-JP') . "\n"; print "6: ". mb_strimwidth($euc_jp, 38, -25,'...','EUC-JP') . "\n"; print "7: ". mb_strimwidth($euc_jp, -30, -25,'...','EUC-JP') . "\n"; -$str = mb_strimwidth($euc_jp, 0, -100,'...','EUC-JP'); -($str === FALSE) ? print "10 OK\n" : print "NG: $str\n"; - -$str = mb_strimwidth($euc_jp, 100, 10,'...','EUC-JP'); -($str === FALSE) ? print "11 OK\n" : print "NG: $str\n"; - -$str = mb_strimwidth($euc_jp, -100, 10,'...','EUC-JP'); -($str === FALSE) ? print "12 OK\n" : print "NG: $str\n"; - -$str = mb_strimwidth($euc_jp, -10, -12,'...','EUC-JP'); -($str === FALSE) ? print "13 OK\n" : print "NG: $str\n"; +try { + var_dump(mb_strimwidth($euc_jp, 0, -100,'...','EUC-JP')); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} +try { + var_dump(mb_strimwidth($euc_jp, 100, 10,'...','EUC-JP')); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} +try { + var_dump(mb_strimwidth($euc_jp, -100, 10,'...','EUC-JP')); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} +try { + var_dump(mb_strimwidth($euc_jp, -10, -12,'...','EUC-JP')); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} ?> ---EXPECTF-- +--EXPECT-- String width: 68 1: 0123¤³¤Îʸ»ú... 2: 0123¤³¤Îʸ»úÎó¤ÏÆüËܸì¤Ç¤¹¡£EUC-JP¤ò»È¤Ã¤Æ¤¤¤Þ¤¹¡£ÆüËܸì¤ÏÌÌÅݽ­¤¤¡£ @@ -43,15 +52,7 @@ String width: 68 5: ¡£ 6: ¡£ 7: ¡£ - -Warning: mb_strimwidth(): Width is out of range in %s on line %d -10 OK - -Warning: mb_strimwidth(): Start position is out of range in %s on line %d -11 OK - -Warning: mb_strimwidth(): Start position is out of range in %s on line %d -12 OK - -Warning: mb_strimwidth(): Width is out of range in %s on line %d -13 OK +mb_strimwidth(): Argument #3 ($width) is out of range +mb_strimwidth(): Argument #2 ($start) is out of range +mb_strimwidth(): Argument #2 ($start) is out of range +mb_strimwidth(): Argument #3 ($width) is out of range