Fixed a bug in zend_memnistr with single character needle

Fixes GH-12457
Closes GH-12458
This commit is contained in:
SakiTakamachi 2023-10-17 22:23:31 +09:00 committed by Ilija Tovilo
parent e3a6dc123d
commit 736032febf
No known key found for this signature in database
GPG Key ID: A4F5D403F118200A
3 changed files with 36 additions and 1 deletions

2
NEWS
View File

@ -4,6 +4,8 @@ PHP NEWS
- Core:
. Fixed double-free of non-interned enum case name. (ilutov)
. Fixed bug GH-12457 (Incorrect result of stripos with single character
needle). (SakiTakamachi)
- DOM:
. Fix registerNodeClass with abstract class crashing. (nielsdos)

33
Zend/tests/gh12457.phpt Normal file
View File

@ -0,0 +1,33 @@
--TEST--
GH-12458 (Fix GH-12457: Fixed a bug in zend_memnistr)
--FILE--
<?php
echo "Test case to ensure the issue is fixed.\n";
var_dump(stripos('aaBBBBBb', 'b'));
var_dump(stripos('aaBBBBBbb', 'b'));
var_dump(stripos('aaBBBBBbbb', 'b'));
var_dump(stristr('aaBBBBBb', 'b'));
var_dump(stristr('aaBBBBBbb', 'b'));
var_dump(stristr('aaBBBBBbbb', 'b'));
echo "\n";
echo "Test cases to ensure the original functionality is not broken.\n";
var_dump(stripos('aaBBBBBbc', 'c'));
var_dump(stripos('aaBBBBBbC', 'c'));
var_dump(stristr('aaBBBBBbc', 'c'));
var_dump(stristr('aaBBBBBbC', 'c'));
?>
--EXPECTF--
Test case to ensure the issue is fixed.
int(2)
int(2)
int(2)
string(6) "BBBBBb"
string(7) "BBBBBbb"
string(8) "BBBBBbbb"
Test cases to ensure the original functionality is not broken.
int(8)
int(8)
string(1) "c"
string(1) "C"

View File

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