Skip to content

Commit 063d787

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix GH-23106: mb_strpos() overreads truncated UTF-8 haystacks (#23107)
2 parents 74e3a69 + 35889a7 commit 063d787

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ PHP NEWS
1111
registrations are freed while still reachable from the cycle collector.
1212
(Ilia Alshanetsky)
1313

14+
- MBString:
15+
. Fixed bug GH-23106 (mb_strpos() reads past the end of a haystack ending in
16+
a truncated UTF-8 sequence). (Lazizbek Ergashev)
17+
1418
- SPL:
1519
. Fixed bug GH-23385 (SplDoublyLinkedList::serialize() use-after-free when
1620
__serialize() removes an element). (David Carlier)

‎ext/mbstring/mbstring.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,9 @@ static unsigned char* offset_to_pointer_utf8(unsigned char *str, unsigned char *
19021902
}
19031903
pos += u8_tbl[*pos];
19041904
}
1905+
if (pos > end) {
1906+
pos = end;
1907+
}
19051908
return pos;
19061909
}
19071910
}

‎ext/mbstring/tests/gh23106.phpt‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-23106 (mb_strpos() reads past the end of a haystack ending in a truncated UTF-8 sequence)
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
var_dump(mb_strpos("AA\xf0\x90", "xyz", 3));
8+
var_dump(mb_strpos("AA\xf0\x90", "x", 3));
9+
var_dump(mb_strrpos("AA\xf0\x90", "A", 3));
10+
var_dump(mb_strrpos("AA\xf0\x90", "A", -1));
11+
try {
12+
mb_strpos("AA\xf0\x90", "xyz", 4);
13+
} catch (ValueError $e) {
14+
echo $e->getMessage(), "\n";
15+
}
16+
?>
17+
--EXPECT--
18+
bool(false)
19+
bool(false)
20+
bool(false)
21+
int(1)
22+
mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)

0 commit comments

Comments
 (0)