Skip to content
Open
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: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ PHP NEWS
- Date:
. Fixed leak on double DatePeriod::__construct() call. (ilutov)

- MBString:
. Fixed bug GH-22779 (mb_strrpos() returns the wrong position for a negative
offset in a non-UTF-8 encoding). (Eyüp Can Akman)

30 Jul 2026, PHP 8.4.24

- Calendar:
Expand Down
2 changes: 1 addition & 1 deletion ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ static size_t mb_find_strpos(zend_string *haystack, zend_string *needle, const m
} else if (offset >= 0) {
found_pos = zend_memnrstr((const char*)offset_pointer, ZSTR_VAL(needle_u8), ZSTR_LEN(needle_u8), ZSTR_VAL(haystack_u8) + ZSTR_LEN(haystack_u8));
} else {
size_t needle_len = pointer_to_offset_utf8((unsigned char*)ZSTR_VAL(needle), (unsigned char*)ZSTR_VAL(needle) + ZSTR_LEN(needle));
size_t needle_len = pointer_to_offset_utf8((unsigned char*)ZSTR_VAL(needle_u8), (unsigned char*)ZSTR_VAL(needle_u8) + ZSTR_LEN(needle_u8));
offset_pointer = offset_to_pointer_utf8(offset_pointer, (unsigned char*)ZSTR_VAL(haystack_u8) + ZSTR_LEN(haystack_u8), needle_len);
if (!offset_pointer) {
offset_pointer = (unsigned char*)ZSTR_VAL(haystack_u8) + ZSTR_LEN(haystack_u8);
Expand Down
34 changes: 34 additions & 0 deletions ext/mbstring/tests/gh22779.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
GH-22779: mb_strrpos() wrong result for a negative offset in a non-UTF-8 encoding
--EXTENSIONS--
mbstring
--FILE--
<?php
/* mb_strrpos() with a negative offset must return the correct position in non-UTF-8 encodings. */
$haystack = "\xA9\xA9X";
$needle = "\xA9";
foreach ([-1, -2, -3] as $offset) {
var_dump(mb_strrpos($haystack, $needle, $offset, 'ISO-8859-1'));
}
var_dump(mb_strrpos("X\xA9", "\xA9", -1, 'ISO-8859-1'));
var_dump(mb_strrpos("\x95Z\x95Z", "\x95", -1, 'Windows-1252'));
var_dump(mb_strrpos("\x95Z\x95Z", "\x95", -2, 'Windows-1252'));
// Two-byte needle in a single-byte encoding.
var_dump(mb_strrpos("\xA9\xB0\xA9\xB0Z", "\xA9\xB0", -2, 'ISO-8859-1'));
// Multibyte: "ああA" in Shift_JIS, needle "あ" (\x82\xA0).
var_dump(mb_strrpos("\x82\xA0\x82\xA0A", "\x82\xA0", -2, 'SJIS'));
var_dump(mb_strrpos("\x82\xA0\x82\xA0A", "\x82\xA0", -3, 'SJIS'));
// UTF-16: an ASCII needle miscounts the other way.
var_dump(mb_strrpos("\x00A\x00X\x00A", "\x00A", -2, 'UTF-16BE'));
?>
--EXPECT--
int(1)
int(1)
int(0)
int(1)
int(2)
int(2)
int(2)
int(1)
int(0)
int(0)
Loading