diff --git a/runtime-common/stdlib/string/string-functions.cpp b/runtime-common/stdlib/string/string-functions.cpp index 57fb4ebd77..413b4c2fc5 100644 --- a/runtime-common/stdlib/string/string-functions.cpp +++ b/runtime-common/stdlib/string/string-functions.cpp @@ -1241,12 +1241,12 @@ string f$stripslashes(const string& str) noexcept { Optional f$stripos(const string& haystack, const string& needle, int64_t offset) noexcept { if (offset < 0) { - php_warning("Wrong offset = %" PRIi64 " in function stripos", offset); - return false; + offset += haystack.size(); } - if (offset >= haystack.size()) { + if (offset < 0 || offset >= haystack.size()) { return false; } + if (needle.size() == 0) { php_warning("Parameter needle is empty in function stripos"); return false; @@ -1726,12 +1726,12 @@ int64_t f$strnatcmp(const string& lhs, const string& rhs) noexcept { Optional f$strpos(const string& haystack, const string& needle, int64_t offset) noexcept { if (offset < 0) { - php_warning("Wrong offset = %" PRIi64 " in function strpos", offset); - return false; + offset += haystack.size(); } - if (offset > int64_t{haystack.size()}) { + if (offset < 0 || offset >= haystack.size()) { return false; } + if (needle.size() <= 1) { if (needle.size() == 0) { php_warning("Parameter needle is empty in function strpos"); diff --git a/tests/phpt/dl/491_strpos.php b/tests/phpt/dl/491_strpos.php index 1f0ae2ff20..7fc701ef4e 100644 --- a/tests/phpt/dl/491_strpos.php +++ b/tests/phpt/dl/491_strpos.php @@ -52,6 +52,27 @@ var_dump (mb_strtoupper($str, 'UTF-8')); // var_dump(strtolower ("ÈÔÛÛÀÏÂÀ¨")); + var_dump(strpos("abcdABCDefghEFGHefgh", "efgh", -10)); + var_dump(strpos("abcdABCDefghEFGHefgh", "efgh", -16)); + var_dump(strpos("abcdABCDefghEFGh_H", "h", -10)); + var_dump(strpos("abcdABCDefghEFGh_H", "h", -5)); + var_dump(strpos("abcdABCDefghEFGh_H", "h", -2)); + var_dump(strpos("0123456789a123456789b123456789c", "7", -5)); + var_dump(strpos("0123456789a123456789b123456789c", "7", -25)); + var_dump(strpos("0123456789a123456789b123456789c", "c", -1)); + var_dump(strpos("0123456789a123456789b123456789c", "9", -1)); + + var_dump(stripos("abcdABCDefghEFGHefgh", "efgh", -10)); + var_dump(stripos("abcdABCDefghEFGHefgh", "efgh", -16)); + var_dump(stripos("abcdABCDefghEFGh_H", "h", -10)); + var_dump(stripos("abcdABCDefghEFGh_H", "h", -5)); + var_dump(stripos("abcdABCDefghEFGh_H", "h", -2)); + var_dump(stripos("0123456789a123456789b123456789c", "7", -5)); + var_dump(stripos("0123456789a123456789b123456789c", "7", -25)); + var_dump(stripos("0123456789a123456789b123456789c", "c", -1)); + var_dump(stripos("0123456789a123456789b123456789c", "9", -1)); + + $a = array (" ", "The quick brown fox jumped over the lazy dog.", "A very long woooooooooooord.", "A very long wooooooooooooord.", "A very long woooooooooooord.", "", "A very long wooooooooooooord.", "A very long wooooooooooooord 234234234 e r we we r we rwe f w ew r wer we wer wer we r wer ."); foreach ($a as $str) { var_dump (nl2br (wordwrap ($str, 1, "\n", true)));