From 401b744498dca09b90f437f82d4f37cc2904ea96 Mon Sep 17 00:00:00 2001 From: SakiTakamachi Date: Tue, 17 Oct 2023 22:23:31 +0900 Subject: [PATCH] Fixed a bug in zend_memnistr add test GH-12457 add test pattern GH-12457 Fix order of elvis of zend_memnistr add more test cases for Fix GH-12457 Fixed test expectations for GH-12457 --- Zend/tests/gh12457.phpt | 33 +++++++++++++++++++++++++++++++++ Zend/zend_operators.h | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Zend/tests/gh12457.phpt diff --git a/Zend/tests/gh12457.phpt b/Zend/tests/gh12457.phpt new file mode 100644 index 0000000000000..da9165f4befc3 --- /dev/null +++ b/Zend/tests/gh12457.phpt @@ -0,0 +1,33 @@ +--TEST-- +GH-12458 (Fix GH-12457: Fixed a bug in zend_memnistr) +--FILE-- + +--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" diff --git a/Zend/zend_operators.h b/Zend/zend_operators.h index 0fc503f2c12b4..e98956239ec3b 100644 --- a/Zend/zend_operators.h +++ b/Zend/zend_operators.h @@ -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;