From: Amos Jeffries <> Date: Thu, 2 Jan 2014 23:03:20 +0000 (+0100) Subject: Fix SBuf::rfind X-Git-Tag: SQUID_3_5_0_1~441 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=22a70610ed31f8ac4799f515d7e9ca78b01e3153;p=thirdparty%2Fsquid.git Fix SBuf::rfind --- diff --git a/src/SBuf.cc b/src/SBuf.cc index 9d2eecd72d..0951533131 100644 --- a/src/SBuf.cc +++ b/src/SBuf.cc @@ -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())) {