Skip to content

Commit 11ce508

Browse files
committed
Fix #77367: Negative size parameter in mb_split
When adding the last element to the result value of `mb_split`, the `chunk_pos` may point beyond the end of the string, in which case the unsigned `n` would underflow. Therefore, we check whether this is the case in the first place, and only calculate `n` otherwise. Since `n` is no longer used outside the block, we move its declaration inside. (cherry picked from commit e617f03)
1 parent a15af81 commit 11ce508

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ PHP NEWS
3131
use-after-free). (cmb)
3232
. Fixed bug #77270 (imagecolormatch Out Of Bounds Write on Heap). (cmb)
3333

34+
- MBString:
35+
. Fixed bug #77367 (Negative size parameter in mb_split). (Stas)
36+
3437
- OCI8:
3538
. Fixed bug #76804 (oci_pconnect with OCI_CRED_EXT not working). (KoenigsKind)
3639
. Added oci_set_call_timeout() for call timeouts.

ext/mbstring/php_mbregex.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,6 @@ PHP_FUNCTION(mb_split)
12381238
size_t string_len;
12391239

12401240
int err;
1241-
size_t n;
12421241
zend_long count = -1;
12431242

12441243
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l", &arg_pattern, &arg_pattern_len, &string, &string_len, &count) == FAILURE) {
@@ -1296,8 +1295,8 @@ PHP_FUNCTION(mb_split)
12961295
}
12971296

12981297
/* otherwise we just have one last element to add to the array */
1299-
n = ((OnigUChar *)(string + string_len) - chunk_pos);
1300-
if (n > 0) {
1298+
if ((OnigUChar *)(string + string_len) > chunk_pos) {
1299+
size_t n = ((OnigUChar *)(string + string_len) - chunk_pos);
13011300
add_next_index_stringl(return_value, (char *)chunk_pos, n);
13021301
} else {
13031302
add_next_index_stringl(return_value, "", 0);

ext/mbstring/tests/bug77367.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #77367 (Negative size parameter in mb_split)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('mbstring')) die('mbstring extension not available');
6+
if (!function_exists('mb_split')) die('mb_split() not available');
7+
?>
8+
--FILE--
9+
<?php
10+
mb_regex_encoding('UTF-8');
11+
var_dump(mb_split("\\w", "\xfc"));
12+
?>
13+
===DONE===
14+
--EXPECT--
15+
array(2) {
16+
[0]=>
17+
string(0) ""
18+
[1]=>
19+
string(0) ""
20+
}
21+
===DONE===

0 commit comments

Comments
 (0)