]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Fix SBuf::rfind
authorAmos Jeffries <>
Thu, 2 Jan 2014 23:03:20 +0000 (00:03 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Thu, 2 Jan 2014 23:03:20 +0000 (00:03 +0100)
src/SBuf.cc

index 9d2eecd72d11898f7a7f41c4bd68c041e29e2079..0951533131df50ddc81b11dbbef7e645439be664 100644 (file)
@@ -630,21 +630,21 @@ SBuf::rfind(const SBuf &needle, SBuf::size_type endPos) const
 
     ++stats.find;
 
-    // on npos input std::string scans from the end of hay
-    if (endPos == npos || endPos > length())
-        endPos = length();
-
-    // on empty hay std::string returns npos
+    // needle is bigger than haystack, impossible find
     if (length() < needle.length())
         return npos;
 
-    // consistent with std::string: on empty needle return min(endpos,length())
+    // if startPos is npos, std::string scans from the end of hay
+    if (endPos == npos || endPos > length()-needle.length())
+        endPos = length()-needle.length();
+
+    // an empty needle found at the end of the haystack
     if (needle.length() == 0)
         return endPos;
 
     char *bufBegin = buf();
     char *cur = bufBegin+endPos;
-    char needleBegin = needle[0];
+    const char needleBegin = needle[0];
     while (cur >= bufBegin) {
         if (*cur == needleBegin) {
             if (0 == memcmp(needle.buf(), cur, needle.length())) {