Skip to content

Commit 0de79a8

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fixed a bug in zend_memnistr with single character needle
2 parents 34948c7 + 736032f commit 0de79a8

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ PHP NEWS
44

55
- Core:
66
. Fixed double-free of non-interned enum case name. (ilutov)
7+
. Fixed bug GH-12457 (Incorrect result of stripos with single character
8+
needle). (SakiTakamachi)
79

810
- DOM:
911
. Fix registerNodeClass with abstract class crashing. (nielsdos)

Zend/tests/gh12457.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
GH-12458 (Fix GH-12457: Fixed a bug in zend_memnistr)
3+
--FILE--
4+
<?php
5+
echo "Test case to ensure the issue is fixed.\n";
6+
var_dump(stripos('aaBBBBBb', 'b'));
7+
var_dump(stripos('aaBBBBBbb', 'b'));
8+
var_dump(stripos('aaBBBBBbbb', 'b'));
9+
var_dump(stristr('aaBBBBBb', 'b'));
10+
var_dump(stristr('aaBBBBBbb', 'b'));
11+
var_dump(stristr('aaBBBBBbbb', 'b'));
12+
13+
echo "\n";
14+
echo "Test cases to ensure the original functionality is not broken.\n";
15+
var_dump(stripos('aaBBBBBbc', 'c'));
16+
var_dump(stripos('aaBBBBBbC', 'c'));
17+
var_dump(stristr('aaBBBBBbc', 'c'));
18+
var_dump(stristr('aaBBBBBbC', 'c'));
19+
?>
20+
--EXPECTF--
21+
Test case to ensure the issue is fixed.
22+
int(2)
23+
int(2)
24+
int(2)
25+
string(6) "BBBBBb"
26+
string(7) "BBBBBbb"
27+
string(8) "BBBBBbbb"
28+
29+
Test cases to ensure the original functionality is not broken.
30+
int(8)
31+
int(8)
32+
string(1) "c"
33+
string(1) "C"

Zend/zend_operators.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ zend_memnistr(const char *haystack, const char *needle, size_t needle_len, const
937937
const char *p_upper = NULL;
938938
if (first_lower != first_upper) {
939939
// If the needle length is 1 we don't need to look beyond p_lower as it is a guaranteed match
940-
size_t upper_search_length = end - (needle_len == 1 && p_lower != NULL ? p_lower : haystack);
940+
size_t upper_search_length = needle_len == 1 && p_lower != NULL ? p_lower - haystack : end - haystack;
941941
p_upper = (const char *)memchr(haystack, first_upper, upper_search_length);
942942
}
943943
const char *p = !p_upper || (p_lower && p_lower < p_upper) ? p_lower : p_upper;

0 commit comments

Comments
 (0)