Skip to content

Commit 736032f

Browse files
SakiTakamachiiluuu1994
authored andcommitted
Fixed a bug in zend_memnistr with single character needle
Fixes GH-12457 Closes GH-12458
1 parent e3a6dc1 commit 736032f

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

NEWS

+2
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

+33
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ zend_memnistr(const char *haystack, const char *needle, size_t needle_len, const
945945
const char *p_upper = NULL;
946946
if (first_lower != first_upper) {
947947
// If the needle length is 1 we don't need to look beyond p_lower as it is a guaranteed match
948-
size_t upper_search_length = end - (needle_len == 1 && p_lower != NULL ? p_lower : haystack);
948+
size_t upper_search_length = needle_len == 1 && p_lower != NULL ? p_lower - haystack : end - haystack;
949949
p_upper = (const char *)memchr(haystack, first_upper, upper_search_length);
950950
}
951951
const char *p = !p_upper || (p_lower && p_lower < p_upper) ? p_lower : p_upper;

0 commit comments

Comments
 (0)