Skip to content

Fix GH-12457: Fixed a bug in zend_memnistr #12458

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 1 commit 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
33 changes: 33 additions & 0 deletions Zend/tests/gh12457.phpt
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion Zend/zend_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, the original code was meant to be this:

		size_t upper_search_length = (needle_len == 1 && p_lower != NULL ? p_lower : end) - haystack;

But your version may be more readable.

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;
Expand Down