Promote some warnings in MBString Regexes

Closes GH-5341
This commit is contained in:
George Peter Banyard 2020-04-02 17:05:43 +02:00
parent 8f415d4413
commit 0444158529
10 changed files with 154 additions and 155 deletions

View File

@ -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;
@ -650,15 +650,14 @@ _php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option
case 'd':
*syntax = ONIG_SYNTAX_POSIX_EXTENDED;
break;
case 'e':
if (eval != NULL) *eval = 1;
break;
default:
break;
zend_value_error("Option \"%c\" is not supported", c);
return false;
}
}
if (option != NULL) *option|=optm;
}
return true;
}
/* }}} */
@ -900,6 +899,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 +924,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 +1005,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 +1013,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 +1040,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 +1114,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 +1245,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 +1290,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 +1345,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 +1372,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 +1477,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 +1554,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 +1586,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 +1626,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;

View File

@ -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

View File

@ -8,13 +8,14 @@ if (!function_exists('mb_ereg')) die('skip mbregex support not available');
--FILE--
<?php
$str = "\x80";
var_dump(
false === mb_eregi('.', $str, $matches),
[] === $matches,
NULL === mb_ereg_replace('.', "\\0", $str),
false === mb_ereg_search_init("\x80", '.'),
false === mb_ereg_search()
);
var_dump(false === mb_eregi('.', $str, $matches));
var_dump([] === $matches);
var_dump(NULL === mb_ereg_replace('.', "\\0", $str));
var_dump(false === mb_ereg_search_init("\x80", '.'));
var_dump(false === mb_ereg_search());
?>
--EXPECT--
bool(true)

View File

@ -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

View File

@ -8,8 +8,17 @@ if (!function_exists('mb_ereg')) die('skip mbregex support not available');
--FILE--
<?php
$var5 = mb_ereg_search_init("","2");
var_dump($var5);
$var6 = mb_eregi_replace("2","","");
$var13 = mb_ereg_search_pos();
var_dump($var6);
try {
$var13 = mb_ereg_search_pos();
} catch (\Error $e) {
echo $e->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

View File

@ -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"

View File

@ -8,6 +8,7 @@ if (!function_exists('mb_split')) die('skip mb_split() not available');
--FILE--
<?php
mb_regex_encoding("UTF-32");
var_dump(mb_split("\x00\x00\x00\x5c\x00\x00\x00B","000000000000000000000000000000"));
?>
--EXPECT--

View File

@ -8,12 +8,21 @@ if (!function_exists('mb_ereg')) die('skip mbregex support not available');
--FILE--
<?php
mb_ereg_search_init("","","");
try {
mb_ereg_search_init("","","");
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
mb_split("","");
mb_ereg_search_regs();
try {
mb_ereg_search_regs();
} catch (\Error $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECTF--
Warning: mb_ereg_search_init(): Empty pattern in %s on line %d
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

View File

@ -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)

View File

@ -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)