Skip to content

Commit a5f21ca

Browse files
committed
Fix phpGH-18901: integer overflow mb_split
We prevent signed overflow by making the count unsigned. The actual interpretation of the count doesn't matter as it's just used to denote a limit. The test output for some limit values looks strange though, so that may need extra investigation. However, that's orthogonal to this fix. Closes phpGH-18906.
1 parent 2694eb9 commit a5f21ca

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ PHP NEWS
1414
. Fixed GH-18902 ldap_exop/ldap_exop_sync assert triggered on empty
1515
request OID. (David Carlier)
1616

17+
- MbString:
18+
. Fixed bug GH-18901 (integer overflow mb_split). (nielsdos)
19+
1720
- Streams:
1821
. Fixed GH-13264 (fgets() and stream_get_line() do not return false on filter
1922
fatal error). (Jakub Zelenka)

ext/mbstring/php_mbregex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ PHP_FUNCTION(mb_split)
11841184
size_t string_len;
11851185

11861186
int err;
1187-
zend_long count = -1;
1187+
zend_ulong count = -1; /* unsigned, it's a limit and we want to prevent signed overflow */
11881188

11891189
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l", &arg_pattern, &arg_pattern_len, &string, &string_len, &count) == FAILURE) {
11901190
RETURN_THROWS();

ext/mbstring/tests/gh18901.phpt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
GH-18901 (integer overflow mb_split)
3+
--EXTENSIONS--
4+
mbstring
5+
--SKIPIF--
6+
<?php
7+
if (!function_exists('mb_split')) die('skip mb_split() not available');
8+
?>
9+
--FILE--
10+
<?php
11+
$vals = [PHP_INT_MIN, PHP_INT_MAX, -1, 0, 1];
12+
foreach ($vals as $val) {
13+
var_dump(mb_split('\d', '123', $val));
14+
}
15+
?>
16+
--EXPECT--
17+
array(4) {
18+
[0]=>
19+
string(0) ""
20+
[1]=>
21+
string(0) ""
22+
[2]=>
23+
string(0) ""
24+
[3]=>
25+
string(0) ""
26+
}
27+
array(4) {
28+
[0]=>
29+
string(0) ""
30+
[1]=>
31+
string(0) ""
32+
[2]=>
33+
string(0) ""
34+
[3]=>
35+
string(0) ""
36+
}
37+
array(4) {
38+
[0]=>
39+
string(0) ""
40+
[1]=>
41+
string(0) ""
42+
[2]=>
43+
string(0) ""
44+
[3]=>
45+
string(0) ""
46+
}
47+
array(1) {
48+
[0]=>
49+
string(3) "123"
50+
}
51+
array(1) {
52+
[0]=>
53+
string(3) "123"
54+
}

0 commit comments

Comments
 (0)