Skip to content

Commit 2f10db3

Browse files
committed
Fix #66797: mb_substr only takes 32-bit signed integer
`from` and `len` are `long`, but get passed to mbfl_substr() which expects `int`s. Therefore we clamp the values to avoid the undefined conversion behavior.
1 parent af7828a commit 2f10db3

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ PHP NEWS
3131
- JSON:
3232
. Fixed bug #72787 (json_decode reads out of bounds). (Jakub Zelenka)
3333

34+
- mbstring:
35+
. Fixed bug #66797 (mb_substr only takes 32-bit signed integer). (cmb)
36+
3437
- MSSQL:
3538
. Fixed bug #72039 (Use of uninitialised value on mssql_guid_string). (Kalle)
3639

ext/mbstring/mbstring.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2799,6 +2799,13 @@ PHP_FUNCTION(mb_substr)
27992799
RETURN_FALSE;
28002800
}
28012801

2802+
if (from > INT_MAX) {
2803+
from = INT_MAX;
2804+
}
2805+
if (len > INT_MAX) {
2806+
len = INT_MAX;
2807+
}
2808+
28022809
ret = mbfl_substr(&string, &result, from, len);
28032810
if (NULL == ret) {
28042811
RETURN_FALSE;

ext/mbstring/tests/bug66797.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Bug #66797 (mb_substr only takes 32-bit signed integer)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('mbstring')) die('skip mbstring extension not available');
6+
if (PHP_INT_SIZE != 8) die('skip this test is for 64bit platforms only');
7+
?>
8+
--FILE--
9+
<?php
10+
var_dump(
11+
mb_substr('bar', 0, 0x7fffffff),
12+
mb_substr('bar', 0, 0x80000000),
13+
mb_substr('bar', 0xffffffff, 1),
14+
mb_substr('bar', 0x100000000, 1)
15+
);
16+
?>
17+
==DONE==
18+
--EXPECTF--
19+
string(3) "bar"
20+
string(3) "bar"
21+
string(0) ""
22+
string(0) ""
23+
==DONE==

0 commit comments

Comments
 (0)