diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2020-10-13 16:17:40 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-10-13 20:40:55 +0200 |
commit | 5582490bf2caa9a416d56378b29c2a2f5e595d60 (patch) | |
tree | 88f9212fcf19dd792b14ab213171065edde7b5a7 /ext/mbstring/php_mbregex.c | |
parent | 5aec24c477e72e71a846361f93ef8861b7a262e5 (diff) | |
download | php-git-5582490bf2caa9a416d56378b29c2a2f5e595d60.tar.gz |
Normalize mb_ereg() return value
mb_ereg()/mb_eregi() currently have an inconsistent return value
based on whether the $matches parameter is passed or not:
> Returns the byte length of the matched string if a match for
> pattern was found in string, or FALSE if no matches were found
> or an error occurred.
>
> If the optional parameter regs was not passed or the length of
> the matched string is 0, this function returns 1.
Coupling this behavior to the $matches parameter doesn't make sense
-- we know the match length either way, there is no technical
reason to distinguish them. However, returning the match length
is not particularly useful either, especially due to the need to
convert 0-length into 1-length to satisfy "truthy" checks. We
could always return 1, which would kind of match the behavior of
preg_match() -- however, preg_match() actually returns the number
of matches, which is 0 or 1 for preg_match(), while false signals
an error. However, mb_ereg() returns false both for no match and
for an error. This would result in an odd 1|false return value.
The patch canonicalizes mb_ereg() to always return a boolean,
where true indicates a match and false indicates no match or error.
This also matches the behavior of the mb_ereg_match() and
mb_ereg_search() functions.
This fixes the default value integrity violation in PHP 8.
Closes GH-6331.
Diffstat (limited to 'ext/mbstring/php_mbregex.c')
-rw-r--r-- | ext/mbstring/php_mbregex.c | 10 |
1 files changed, 2 insertions, 8 deletions
diff --git a/ext/mbstring/php_mbregex.c b/ext/mbstring/php_mbregex.c index 45aac7baf2..e87a7c6131 100644 --- a/ext/mbstring/php_mbregex.c +++ b/ext/mbstring/php_mbregex.c @@ -891,7 +891,7 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase) size_t arg_pattern_len, string_len; php_mb_regex_t *re; OnigRegion *regs = NULL; - int i, match_len, beg, end; + int i, beg, end; OnigOptionType options; char *str; @@ -938,11 +938,8 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase) goto out; } - match_len = 1; str = string; if (array != NULL) { - - match_len = regs->end[0] - regs->beg[0]; for (i = 0; i < regs->num_regs; i++) { beg = regs->beg[i]; end = regs->end[i]; @@ -959,10 +956,7 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase) } } - if (match_len == 0) { - match_len = 1; - } - RETVAL_LONG(match_len); + RETVAL_TRUE; out: if (regs != NULL) { onig_region_free(regs, 1); |