Skip to content

Commit 5582490

Browse files
committed
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.
1 parent 5aec24c commit 5582490

16 files changed

+74
-77
lines changed

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ PHP 8.0 UPGRADE NOTES
389389
aliases for better interoperability with the iconv extension. The mbregex
390390
ISO 8859 aliases with underscores (ISO_8859_* and ISO8859_*) have also been
391391
removed.
392+
. mb_ereg() and mb_eregi() will now return boolean true on a successfuly
393+
match. Previously they returned integer 1 if $matches was not passed, or
394+
max(1, strlen($reg[0])) is $matches was passed.
392395

393396
- OCI8:
394397
. The OCI-Lob class is now called OCILob, and the OCI-Collection class is now

ext/mbstring/mbstring.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ function mb_chr(int $codepoint, ?string $encoding = null): string|false {}
9393
function mb_regex_encoding(?string $encoding = null): string|bool {}
9494

9595
/** @param array $matches */
96-
function mb_ereg(string $pattern, string $string, &$matches = null): int|false {}
96+
function mb_ereg(string $pattern, string $string, &$matches = null): bool {}
9797

9898
/** @param array $matches */
99-
function mb_eregi(string $pattern, string $string, &$matches = null): int|false {}
99+
function mb_eregi(string $pattern, string $string, &$matches = null): bool {}
100100

101101
function mb_ereg_replace(string $pattern, string $replacement, string $string, ?string $options = null): string|false|null {}
102102

ext/mbstring/mbstring_arginfo.h

Lines changed: 2 additions & 2 deletions
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: 3e5b45cf71fe75bde026062816cb28eceea4aa38 */
2+
* Stub hash: 51f0769423c046d612adf81091192165ad265456 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_language, 0, 0, MAY_BE_STRING|MAY_BE_BOOL)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, language, IS_STRING, 1, "null")
@@ -205,7 +205,7 @@ ZEND_END_ARG_INFO()
205205
#endif
206206

207207
#if defined(HAVE_MBREGEX)
208-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_ereg, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
208+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_ereg, 0, 2, _IS_BOOL, 0)
209209
ZEND_ARG_TYPE_INFO(0, pattern, IS_STRING, 0)
210210
ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0)
211211
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, matches, "null")

ext/mbstring/php_mbregex.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
891891
size_t arg_pattern_len, string_len;
892892
php_mb_regex_t *re;
893893
OnigRegion *regs = NULL;
894-
int i, match_len, beg, end;
894+
int i, beg, end;
895895
OnigOptionType options;
896896
char *str;
897897

@@ -938,11 +938,8 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
938938
goto out;
939939
}
940940

941-
match_len = 1;
942941
str = string;
943942
if (array != NULL) {
944-
945-
match_len = regs->end[0] - regs->beg[0];
946943
for (i = 0; i < regs->num_regs; i++) {
947944
beg = regs->beg[i];
948945
end = regs->end[i];
@@ -959,10 +956,7 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
959956
}
960957
}
961958

962-
if (match_len == 0) {
963-
match_len = 1;
964-
}
965-
RETVAL_LONG(match_len);
959+
RETVAL_TRUE;
966960
out:
967961
if (regs != NULL) {
968962
onig_region_free(regs, 1);

ext/mbstring/tests/bug78633.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (!function_exists('mb_eregi')) die('skip mb_eregi function not available');
88
--FILE--
99
<?php
1010
$res = mb_eregi(".+Isssǰ", ".+Isssǰ");
11-
if ($res === 1 || $res === false) {
11+
if (is_bool($res)) {
1212
echo "ok\n";
1313
} else {
1414
var_dump($res);

ext/mbstring/tests/mb_ereg-compat-02.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function_exists('mb_ereg') or die("skip mb_ereg() is not available in this build
1919
echo "\n";
2020
?>
2121
--EXPECT--
22-
32
22+
1
2323
This is a nice and simple string
2424
is
2525
is

ext/mbstring/tests/mb_ereg.phpt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ output_handler=
3131
}
3232
?>
3333
--EXPECT--
34-
(15)6162632064656620676869206a6b6c2064656620676869206a6b6c
35-
(27)a3e1a3e2a3e320a4a2a4aaa4a420a4aba4b3a4ca20a4efa4f1a4f320a3e1a3e2a3e320a4a2a4aaa4a420a4ab20a4b3a4ca20a4efa4f1a4f3
36-
(27)a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab20a4ada4ab20a4f2a4f020a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab2020a4ada4ab20a4f2a4f0
37-
(15)6162632064656620676869206a6b6c2064656620676869206a6b6c
38-
(27)a3e1a3e2a3e320a4a2a4aaa4a420a4aba4b3a4ca20a4efa4f1a4f320a3e1a3e2a3e320a4a2a4aaa4a420a4ab20a4b3a4ca20a4efa4f1a4f3
39-
(27)a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab20a4ada4ab20a4f2a4f020a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab2020a4ada4ab20a4f2a4f0
40-
(15)6162632064656620676869206a6b6c2064656620676869206a6b6c
41-
(27)a3e1a3e2a3e320a4a2a4aaa4a420a4aba4b3a4ca20a4efa4f1a4f320a3e1a3e2a3e320a4a2a4aaa4a420a4ab20a4b3a4ca20a4efa4f1a4f3
42-
(27)a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab20a4ada4ab20a4f2a4f020a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab2020a4ada4ab20a4f2a4f0
43-
(15)6162632064656620676869206a6b6c2064656620676869206a6b6c
44-
(39)a3e1a3e2a3e320a4a2a4aaa4a420a4aba4b3a4ca20a4efa4f1a4f320a3e1a3e2a3e320a4a2a4aaa4a420a4ab20a4b3a4ca20a4efa4f1a4f3
45-
(39)a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab20a4ada4ab20a4f2a4f020a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab2020a4ada4ab20a4f2a4f0
34+
(1)6162632064656620676869206a6b6c2064656620676869206a6b6c
35+
(1)a3e1a3e2a3e320a4a2a4aaa4a420a4aba4b3a4ca20a4efa4f1a4f320a3e1a3e2a3e320a4a2a4aaa4a420a4ab20a4b3a4ca20a4efa4f1a4f3
36+
(1)a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab20a4ada4ab20a4f2a4f020a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab2020a4ada4ab20a4f2a4f0
37+
(1)6162632064656620676869206a6b6c2064656620676869206a6b6c
38+
(1)a3e1a3e2a3e320a4a2a4aaa4a420a4aba4b3a4ca20a4efa4f1a4f320a3e1a3e2a3e320a4a2a4aaa4a420a4ab20a4b3a4ca20a4efa4f1a4f3
39+
(1)a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab20a4ada4ab20a4f2a4f020a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab2020a4ada4ab20a4f2a4f0
40+
(1)6162632064656620676869206a6b6c2064656620676869206a6b6c
41+
(1)a3e1a3e2a3e320a4a2a4aaa4a420a4aba4b3a4ca20a4efa4f1a4f320a3e1a3e2a3e320a4a2a4aaa4a420a4ab20a4b3a4ca20a4efa4f1a4f3
42+
(1)a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab20a4ada4ab20a4f2a4f020a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab2020a4ada4ab20a4f2a4f0
43+
(1)6162632064656620676869206a6b6c2064656620676869206a6b6c
44+
(1)a3e1a3e2a3e320a4a2a4aaa4a420a4aba4b3a4ca20a4efa4f1a4f320a3e1a3e2a3e320a4a2a4aaa4a420a4ab20a4b3a4ca20a4efa4f1a4f3
45+
(1)a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab20a4ada4ab20a4f2a4f020a3eda3faa3f8a3e6a3f020a4a6a4aaa4ab2020a4ada4ab20a4f2a4f0

ext/mbstring/tests/mb_ereg_basic.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,28 @@ Regex encoding set to utf-8
7676

7777
**-- ASCII String --**
7878
-- Without $regs argument--
79-
int(1)
80-
int(1)
79+
bool(true)
80+
bool(true)
8181
--With $regs argument --
82-
int(36)
82+
bool(true)
8383
array(2) {
8484
[0]=>
8585
string(48) "VGhpcyBpcyBhbiBFbmdsaXNoIHN0cmluZy4gMDEyMzQ1Njc4"
8686
[1]=>
8787
string(24) "VGhpcyBpcyBhbiBFbmdsaXM="
8888
}
89-
int(17)
89+
bool(true)
9090
array(1) {
9191
[0]=>
9292
string(24) "VGhpcyBpcyBhbiBFbmdsaXM="
9393
}
9494

9595
**-- Multibyte String --**
9696
-- Without $regs argument --
97-
int(1)
97+
bool(true)
9898
bool(false)
9999
-- With $regs argument --
100-
int(35)
100+
bool(true)
101101
array(3) {
102102
[0]=>
103103
string(48) "5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzQ="

ext/mbstring/tests/mb_ereg_variation3.phpt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,84 +69,84 @@ echo "Done";
6969
*** Testing mb_ereg() : variation ***
7070

7171
-- Iteration 1 --
72-
int(3)
72+
bool(true)
7373
array(1) {
7474
[0]=>
7575
string(4) "YUIx"
7676
}
7777

7878
-- Iteration 2 --
79-
int(4)
79+
bool(true)
8080
array(1) {
8181
[0]=>
8282
string(8) "YUJjRA=="
8383
}
8484

8585
-- Iteration 3 --
86-
int(4)
86+
bool(true)
8787
array(1) {
8888
[0]=>
8989
string(8) "YWIvPQ=="
9090
}
9191

9292
-- Iteration 4 --
93-
int(2)
93+
bool(true)
9494
array(1) {
9595
[0]=>
9696
string(4) "IAk="
9797
}
9898

9999
-- Iteration 5 --
100-
int(3)
100+
bool(true)
101101
array(1) {
102102
[0]=>
103103
string(4) "MjM0"
104104
}
105105

106106
-- Iteration 6 --
107-
int(9)
107+
bool(true)
108108
array(1) {
109109
[0]=>
110110
string(12) "5pel5pys6Kqe"
111111
}
112112

113113
-- Iteration 7 --
114-
int(4)
114+
bool(true)
115115
array(1) {
116116
[0]=>
117117
string(8) "Zmpkcw=="
118118
}
119119

120120
-- Iteration 8 --
121-
int(9)
121+
bool(true)
122122
array(1) {
123123
[0]=>
124124
string(12) "5pel5pys6Kqe"
125125
}
126126

127127
-- Iteration 9 --
128-
int(5)
128+
bool(true)
129129
array(1) {
130130
[0]=>
131131
string(8) "LiEiKkA="
132132
}
133133

134134
-- Iteration 10 --
135-
int(1)
135+
bool(true)
136136
array(1) {
137137
[0]=>
138138
string(4) "CQ=="
139139
}
140140

141141
-- Iteration 11 --
142-
int(5)
142+
bool(true)
143143
array(1) {
144144
[0]=>
145145
string(8) "SURTSlY="
146146
}
147147

148148
-- Iteration 12 --
149-
int(4)
149+
bool(true)
150150
array(1) {
151151
[0]=>
152152
string(8) "M2I1RA=="

ext/mbstring/tests/mb_ereg_variation4.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ echo "Done";
7272
*** Testing mb_ereg() : usage variations ***
7373

7474
-- Iteration 1 --
75-
int(47)
75+
bool(true)
7676
array(1) {
7777
[0]=>
7878
string(64) "5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZMDEyMzTvvJXvvJbvvJfvvJjvvJk="
7979
}
8080

8181
-- Iteration 2 --
82-
int(27)
82+
bool(true)
8383
array(1) {
8484
[0]=>
8585
string(36) "5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ"
8686
}
8787

8888
-- Iteration 3 --
89-
int(5)
89+
bool(true)
9090
array(1) {
9191
[0]=>
9292
string(8) "MDEyMzQ="
@@ -99,14 +99,14 @@ bool(false)
9999
bool(false)
100100

101101
-- Iteration 6 --
102-
int(20)
102+
bool(true)
103103
array(1) {
104104
[0]=>
105105
string(28) "MDEyMzTvvJXvvJbvvJfvvJjvvJk="
106106
}
107107

108108
-- Iteration 7 --
109-
int(50)
109+
bool(true)
110110
array(1) {
111111
[0]=>
112112
string(68) "5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII="
@@ -116,14 +116,14 @@ array(1) {
116116
bool(false)
117117

118118
-- Iteration 9 --
119-
int(50)
119+
bool(true)
120120
array(1) {
121121
[0]=>
122122
string(68) "5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII="
123123
}
124124

125125
-- Iteration 10 --
126-
int(3)
126+
bool(true)
127127
array(1) {
128128
[0]=>
129129
string(4) "44CC"
@@ -136,7 +136,7 @@ bool(false)
136136
bool(false)
137137

138138
-- Iteration 13 --
139-
int(5)
139+
bool(true)
140140
array(1) {
141141
[0]=>
142142
string(8) "MDEyMzQ="

ext/mbstring/tests/mb_ereg_variation5.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ function base64_encode_var_dump($regs) {
5959
--EXPECT--
6060
*** Testing mb_ereg() : usage variations ***
6161

62-
ASCII String without $regs arg: int(1)
62+
ASCII String without $regs arg: bool(true)
6363
ASCII String with $regs arg:
64-
int(38)
64+
bool(true)
6565
array(1) {
6666
[0]=>
6767
string(52) "VGhpcyBpcyBhbiBFbmdsaXNoIHN0cmluZy4gMDEyMzQ1Njc4OS4="
6868
}
6969

70-
Multibyte String without $regs arg: int(1)
70+
Multibyte String without $regs arg: bool(true)
7171
Multubyte String with $regs arg:
72-
int(53)
72+
bool(true)
7373
array(1) {
7474
[0]=>
7575
string(72) "5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII="

0 commit comments

Comments
 (0)