diff --git a/ext/mbstring/php_mbregex.c b/ext/mbstring/php_mbregex.c index b5bd79fbe4399..2b489b5515131 100644 --- a/ext/mbstring/php_mbregex.c +++ b/ext/mbstring/php_mbregex.c @@ -591,8 +591,8 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT /* }}} */ /* {{{ _php_mb_regex_init_options */ -static void -_php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option, OnigSyntaxType **syntax, int *eval) +static bool _php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option, + OnigSyntaxType **syntax) { size_t n; char c; @@ -651,14 +651,16 @@ _php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option *syntax = ONIG_SYNTAX_POSIX_EXTENDED; break; case 'e': - if (eval != NULL) *eval = 1; - break; + zend_value_error("Option \"e\" is not supported"); + return false; default: - break; + zend_value_error("Option \"%c\" is not supported", c); + return false; } } if (option != NULL) *option|=optm; } + return true; } /* }}} */ @@ -900,6 +902,11 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase) RETURN_THROWS(); } + if (arg_pattern_len == 0) { + zend_argument_value_error(1, "must not be empty"); + RETURN_THROWS(); + } + if (array != NULL) { array = zend_try_array_init(array); if (!array) { @@ -920,12 +927,6 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase) options |= ONIG_OPTION_IGNORECASE; } - if (arg_pattern_len == 0) { - php_error_docref(NULL, E_WARNING, "Empty pattern"); - RETVAL_FALSE; - goto out; - } - re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, options, MBREX(regex_default_syntax)); if (re == NULL) { RETVAL_FALSE; @@ -1007,7 +1008,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp smart_str out_buf = {0}; smart_str eval_buf = {0}; smart_str *pbuf; - int err, eval, n; + int err, n; OnigUChar *pos; OnigUChar *string_lim; char *description = NULL; @@ -1015,7 +1016,6 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp const mbfl_encoding *enc = php_mb_regex_get_mbctype_encoding(); ZEND_ASSERT(enc != NULL); - eval = 0; { char *option_str = NULL; size_t option_str_len = 0; @@ -1043,20 +1043,15 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp } if (option_str != NULL) { - _php_mb_regex_init_options(option_str, option_str_len, &options, &syntax, &eval); + /* Initialize option and in case of failure it means there is a value error */ + if (!_php_mb_regex_init_options(option_str, option_str_len, &options, &syntax)) { + RETURN_THROWS(); + } } else { options |= MBREX(regex_default_options); syntax = MBREX(regex_default_syntax); } } - if (eval) { - if (is_callable) { - php_error_docref(NULL, E_WARNING, "Option 'e' cannot be used with replacement callback"); - } else { - php_error_docref(NULL, E_WARNING, "The 'e' option is no longer supported, use mb_ereg_replace_callback instead"); - } - RETURN_FALSE; - } /* create regex pattern buffer */ re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, options, syntax); @@ -1122,7 +1117,9 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp zval_ptr_dtor(&retval); } else { if (!EG(exception)) { - php_error_docref(NULL, E_WARNING, "Unable to call custom replacement function"); + zend_throw_error(NULL, "Unable to call custom replacement function"); + zval_ptr_dtor(&subpats); + RETURN_THROWS(); } } zval_ptr_dtor(&subpats); @@ -1251,6 +1248,7 @@ PHP_FUNCTION(mb_split) onig_region_free(regs, 1); /* see if we encountered an error */ + // ToDo investigate if this can actually/should happen ... if (err <= -2) { OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN]; onig_error_code_to_str(err_str, err); @@ -1295,7 +1293,9 @@ PHP_FUNCTION(mb_ereg_match) } if (option_str != NULL) { - _php_mb_regex_init_options(option_str, option_str_len, &option, &syntax, NULL); + if(!_php_mb_regex_init_options(option_str, option_str_len, &option, &syntax)) { + RETURN_THROWS(); + } } else { option |= MBREX(regex_default_options); syntax = MBREX(regex_default_syntax); @@ -1348,7 +1348,7 @@ static void _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mod } if (arg_options) { - _php_mb_regex_init_options(arg_options, arg_options_len, &option, &syntax, NULL); + _php_mb_regex_init_options(arg_options, arg_options_len, &option, &syntax); } else { option |= MBREX(regex_default_options); syntax = MBREX(regex_default_syntax); @@ -1375,13 +1375,13 @@ static void _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mod } if (MBREX(search_re) == NULL) { - php_error_docref(NULL, E_WARNING, "No regex given"); - RETURN_FALSE; + zend_throw_error(NULL, "No pattern was provided"); + RETURN_THROWS(); } if (str == NULL) { - php_error_docref(NULL, E_WARNING, "No string given"); - RETURN_FALSE; + zend_throw_error(NULL, "No string was provided"); + RETURN_THROWS(); } MBREX(search_regs) = onig_region_new(); @@ -1480,13 +1480,13 @@ PHP_FUNCTION(mb_ereg_search_init) } if (arg_pattern && arg_pattern_len == 0) { - php_error_docref(NULL, E_WARNING, "Empty pattern"); - RETURN_FALSE; + zend_argument_value_error(2, "must not be empty"); + RETURN_THROWS(); } if (arg_options) { option = 0; - _php_mb_regex_init_options(arg_options, arg_options_len, &option, &syntax, NULL); + _php_mb_regex_init_options(arg_options, arg_options_len, &option, &syntax); } else { option = MBREX(regex_default_options); syntax = MBREX(regex_default_syntax); @@ -1557,6 +1557,7 @@ PHP_FUNCTION(mb_ereg_search_getregs) onig_foreach_name(MBREX(search_re), mb_regex_groups_iter, &args); } } else { + // TODO This seems to be some logical error, promote to Error RETVAL_FALSE; } } @@ -1588,12 +1589,12 @@ PHP_FUNCTION(mb_ereg_search_setpos) } if (position < 0 || (!Z_ISUNDEF(MBREX(search_str)) && Z_TYPE(MBREX(search_str)) == IS_STRING && (size_t)position > Z_STRLEN(MBREX(search_str)))) { - php_error_docref(NULL, E_WARNING, "Position is out of range"); - MBREX(search_pos) = 0; - RETURN_FALSE; + zend_argument_value_error(1, "is out of range"); + RETURN_THROWS(); } MBREX(search_pos) = position; + // TODO Return void RETURN_TRUE; } /* }}} */ @@ -1628,7 +1629,9 @@ PHP_FUNCTION(mb_regex_set_options) if (string != NULL) { opt = 0; syntax = NULL; - _php_mb_regex_init_options(string, string_len, &opt, &syntax, NULL); + if(!_php_mb_regex_init_options(string, string_len, &opt, &syntax)) { + RETURN_THROWS(); + } _php_mb_regex_set_options(opt, syntax, &prev_opt, &prev_syntax); opt = prev_opt; syntax = prev_syntax; diff --git a/ext/mbstring/tests/bug43994.phpt b/ext/mbstring/tests/bug43994.phpt index 96c862e6970cf..d41bf90a9fc83 100644 --- a/ext/mbstring/tests/bug43994.phpt +++ b/ext/mbstring/tests/bug43994.phpt @@ -25,106 +25,76 @@ foreach($inputs as $input) { } echo "\n-- Iteration $iterator --\n"; echo "Without \$regs arg:\n"; - var_dump( mb_ereg($input, 'hello, world') ); + try { + var_dump( mb_ereg($input, 'hello, world') ); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } + echo "With \$regs arg:\n"; - var_dump(mb_ereg($input, 'hello, world', $mb_regs)); + try { + var_dump(mb_ereg($input, 'hello, world', $mb_regs)); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } + var_dump($mb_regs); $iterator++; }; ?> ---EXPECTF-- +--EXPECT-- -- Iteration 1 -- Without $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) +mb_ereg(): Argument #1 ($pattern) must not be empty With $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) -array(0) { -} +mb_ereg(): Argument #1 ($pattern) must not be empty +NULL -- Iteration 2 -- Without $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) +mb_ereg(): Argument #1 ($pattern) must not be empty With $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) -array(0) { -} +mb_ereg(): Argument #1 ($pattern) must not be empty +NULL -- Iteration 3 -- Without $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) +mb_ereg(): Argument #1 ($pattern) must not be empty With $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) -array(0) { -} +mb_ereg(): Argument #1 ($pattern) must not be empty +NULL -- Iteration 4 -- Without $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) +mb_ereg(): Argument #1 ($pattern) must not be empty With $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) -array(0) { -} +mb_ereg(): Argument #1 ($pattern) must not be empty +NULL -- Iteration 5 -- Without $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) +mb_ereg(): Argument #1 ($pattern) must not be empty With $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) -array(0) { -} +mb_ereg(): Argument #1 ($pattern) must not be empty +NULL -- Iteration 6 -- Without $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) +mb_ereg(): Argument #1 ($pattern) must not be empty With $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) -array(0) { -} +mb_ereg(): Argument #1 ($pattern) must not be empty +NULL -- Iteration 7 -- Without $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) +mb_ereg(): Argument #1 ($pattern) must not be empty With $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) -array(0) { -} +mb_ereg(): Argument #1 ($pattern) must not be empty +NULL -- Iteration 8 -- Without $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) +mb_ereg(): Argument #1 ($pattern) must not be empty With $regs arg: - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) -array(0) { -} +mb_ereg(): Argument #1 ($pattern) must not be empty +NULL diff --git a/ext/mbstring/tests/bug69151.phpt b/ext/mbstring/tests/bug69151.phpt index a839e8aa74f5a..a91525c3067ff 100644 --- a/ext/mbstring/tests/bug69151.phpt +++ b/ext/mbstring/tests/bug69151.phpt @@ -8,13 +8,14 @@ if (!function_exists('mb_ereg')) die('skip mbregex support not available'); --FILE-- --EXPECT-- bool(true) diff --git a/ext/mbstring/tests/bug72164.phpt b/ext/mbstring/tests/bug72164.phpt index f90fe89938d2f..eff18982d5ea5 100644 --- a/ext/mbstring/tests/bug72164.phpt +++ b/ext/mbstring/tests/bug72164.phpt @@ -10,9 +10,13 @@ if (!function_exists('mb_ereg')) die('skip mbregex support not available'); $var0 = "e"; $var2 = ""; $var3 = NULL; -$var8 = mb_ereg_replace($var2,$var3,$var3,$var0); -var_dump($var8); +try { + $var8 = mb_ereg_replace($var2,$var3,$var3,$var0); + var_dump($var8); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} + ?> ---EXPECTF-- -Warning: mb_ereg_replace(): The 'e' option is no longer supported, use mb_ereg_replace_callback instead in %s on line %d -bool(false) +--EXPECT-- +Option "e" is not supported diff --git a/ext/mbstring/tests/bug72399.phpt b/ext/mbstring/tests/bug72399.phpt index b50ca7369801b..f5bd4396d30aa 100644 --- a/ext/mbstring/tests/bug72399.phpt +++ b/ext/mbstring/tests/bug72399.phpt @@ -8,8 +8,17 @@ if (!function_exists('mb_ereg')) die('skip mbregex support not available'); --FILE-- getMessage() . \PHP_EOL; +} ?> ---EXPECTF-- -Warning: mb_ereg_search_pos(): No regex given in %sbug72399.php on line %d +--EXPECT-- +bool(true) +string(0) "" +No pattern was provided diff --git a/ext/mbstring/tests/bug76999.phpt b/ext/mbstring/tests/bug76999.phpt index 5ba9f0e8e38b0..b631a2041d796 100644 --- a/ext/mbstring/tests/bug76999.phpt +++ b/ext/mbstring/tests/bug76999.phpt @@ -11,12 +11,16 @@ mb_regex_set_options("pr"); var_dump(mb_regex_set_options("m")); var_dump(mb_regex_set_options("mdi")); var_dump(mb_regex_set_options("m")); -var_dump(mb_regex_set_options("a")); +try { + var_dump(mb_regex_set_options("a")); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} var_dump(mb_regex_set_options()); ?> --EXPECT-- string(2) "pr" string(2) "mr" string(3) "imd" +Option "a" is not supported string(2) "mr" -string(1) "r" diff --git a/ext/mbstring/tests/bug77418.phpt b/ext/mbstring/tests/bug77418.phpt index 4e3130bdd1bc9..1f00371057db2 100644 --- a/ext/mbstring/tests/bug77418.phpt +++ b/ext/mbstring/tests/bug77418.phpt @@ -8,6 +8,7 @@ if (!function_exists('mb_split')) die('skip mb_split() not available'); --FILE-- --EXPECT-- diff --git a/ext/mbstring/tests/empty_pattern.phpt b/ext/mbstring/tests/empty_pattern.phpt index 019ccba02c51d..c5834a6cad4eb 100644 --- a/ext/mbstring/tests/empty_pattern.phpt +++ b/ext/mbstring/tests/empty_pattern.phpt @@ -8,12 +8,21 @@ if (!function_exists('mb_ereg')) die('skip mbregex support not available'); --FILE-- getMessage() . \PHP_EOL; +} + mb_split("",""); -mb_ereg_search_regs(); -?> ---EXPECTF-- -Warning: mb_ereg_search_init(): Empty pattern in %s on line %d +try { + mb_ereg_search_regs(); +} catch (\Error $e) { + echo $e->getMessage() . \PHP_EOL; +} -Warning: mb_ereg_search_regs(): No regex given in %s on line %d +?> +--EXPECT-- +mb_ereg_search_init(): Argument #2 ($pattern) must not be empty +No pattern was provided diff --git a/ext/mbstring/tests/mb_ereg1.phpt b/ext/mbstring/tests/mb_ereg1.phpt index 5fa830fa63bfa..653df93ce0638 100644 --- a/ext/mbstring/tests/mb_ereg1.phpt +++ b/ext/mbstring/tests/mb_ereg1.phpt @@ -16,13 +16,13 @@ $a = array( foreach ($a as $args) { try { var_dump(mb_ereg($args[0], $args[1], $args[2])); - } catch (TypeError $e) { - echo $e->getMessage(), "\n"; + } catch (\TypeError|\ValueError $e) { + echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL; } var_dump($args); } ?> ---EXPECTF-- +--EXPECT-- bool(false) array(3) { [0]=> @@ -33,19 +33,16 @@ array(3) { array(0) { } } - -Warning: mb_ereg(): Empty pattern in %s on line %d -bool(false) +ValueError: mb_ereg(): Argument #1 ($pattern) must not be empty array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> - array(0) { - } + string(0) "" } -mb_ereg(): Argument #1 ($pattern) must be of type string, array given +TypeError: mb_ereg(): Argument #1 ($pattern) must be of type string, array given array(3) { [0]=> array(0) { @@ -55,7 +52,7 @@ array(3) { [2]=> string(0) "" } -mb_ereg(): Argument #2 ($string) must be of type string, array given +TypeError: mb_ereg(): Argument #2 ($string) must be of type string, array given array(3) { [0]=> int(1) diff --git a/ext/mbstring/tests/mb_ereg_search_setpos.phpt b/ext/mbstring/tests/mb_ereg_search_setpos.phpt index 0a90d005dd8f7..3b73025489fc2 100644 --- a/ext/mbstring/tests/mb_ereg_search_setpos.phpt +++ b/ext/mbstring/tests/mb_ereg_search_setpos.phpt @@ -11,22 +11,32 @@ mb_regex_encoding('iso-8859-1'); $test_str = 'Iñtërnâtiônàlizætiøn'; // Length = 20 var_dump(mb_ereg_search_setpos(50)); // OK -var_dump(mb_ereg_search_setpos(-1)); // Error +try { + var_dump(mb_ereg_search_setpos(-1)); // Error +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} mb_ereg_search_init($test_str); $positions = array( 5, 20, 21, 25, 0, -5, -20, -30); foreach($positions as $pos) { echo("\n* Position: $pos :\n"); - var_dump(mb_ereg_search_setpos($pos)); - var_dump(mb_ereg_search_getpos()); + try { + var_dump(mb_ereg_search_setpos($pos)); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } + try { + var_dump(mb_ereg_search_getpos()); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } } ?> ---EXPECTF-- +--EXPECT-- bool(true) - -Warning: mb_ereg_search_setpos(): Position is out of range in %s on line %d -bool(false) +mb_ereg_search_setpos(): Argument #1 ($position) is out of range * Position: 5 : bool(true) @@ -37,16 +47,12 @@ bool(true) int(20) * Position: 21 : - -Warning: mb_ereg_search_setpos(): Position is out of range in %s on line %d -bool(false) -int(0) +mb_ereg_search_setpos(): Argument #1 ($position) is out of range +int(20) * Position: 25 : - -Warning: mb_ereg_search_setpos(): Position is out of range in %s on line %d -bool(false) -int(0) +mb_ereg_search_setpos(): Argument #1 ($position) is out of range +int(20) * Position: 0 : bool(true) @@ -61,7 +67,5 @@ bool(true) int(0) * Position: -30 : - -Warning: mb_ereg_search_setpos(): Position is out of range in %s on line %d -bool(false) +mb_ereg_search_setpos(): Argument #1 ($position) is out of range int(0)