Skip to content

Fix segfault in mb_strrpos/mb_strripos with ASCII encoding and negative offset #11220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/mbstring/libmbfl/mbfl/mbfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ mbfl_strpos(
const unsigned char *offset_pointer;

if (haystack->encoding->no_encoding != mbfl_no_encoding_utf8) {
mbfl_string_init(&_haystack_u8);
mbfl_string_init_set(&_haystack_u8, haystack->encoding);
haystack_u8 = mbfl_convert_encoding(haystack, &_haystack_u8, &mbfl_encoding_utf8);
if (haystack_u8 == NULL) {
result = MBFL_ERROR_ENCODING;
Expand All @@ -605,7 +605,7 @@ mbfl_strpos(
}

if (needle->encoding->no_encoding != mbfl_no_encoding_utf8) {
mbfl_string_init(&_needle_u8);
mbfl_string_init_set(&_needle_u8, needle->encoding);
needle_u8 = mbfl_convert_encoding(needle, &_needle_u8, &mbfl_encoding_utf8);
if (needle_u8 == NULL) {
result = MBFL_ERROR_ENCODING;
Expand Down
12 changes: 12 additions & 0 deletions ext/mbstring/tests/gh11217.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
GH-11217: Segfault in mb_strrpos/mb_strripos with ASCII encoding and negative offset
--EXTENSIONS--
mbstring
--FILE--
<?php
var_dump(mb_strrpos('foo', 'foo', -1, 'ASCII'));
var_dump(mb_strripos('foo', 'foo', -1, 'ASCII'));
?>
--EXPECT--
int(0)
int(0)
6 changes: 6 additions & 0 deletions ext/mbstring/tests/mb_strrpos_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var_dump(mb_strrpos($string_ascii, 'is', 4, 'ISO-8859-1'));
echo "\n-- ASCII string 2 --\n";
var_dump(mb_strrpos($string_ascii, 'hello, world'));

echo "\n-- ASCII string with negative offset --\n";
var_dump(mb_strrpos($string_ascii, 'hello', -1, 'ISO-8859-1'));

echo "\n-- Multibyte string 1 --\n";
$needle1 = base64_decode('44CC');
var_dump(mb_strrpos($string_mb, $needle1));
Expand All @@ -41,6 +44,9 @@ int(15)
-- ASCII string 2 --
bool(false)

-- ASCII string with negative offset --
bool(false)

-- Multibyte string 1 --
int(20)

Expand Down