]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
When SBuf chop()s away everything, always clear the buffer.
authorAlex Rousskov <rousskov@measurement-factory.com>
Sun, 26 Jul 2015 18:16:34 +0000 (12:16 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Sun, 26 Jul 2015 18:16:34 +0000 (12:16 -0600)
The old code was trying to make the clearance decision without
normalizing parameters first and missed most cases as the result.

In theory, clear()ing SBuf during chop() is just an optimization
that should have no effect on correct code functionality.

src/SBuf.cc

index 1c40c9c95e7fff1d846c40545cc9ef8854baadc4..412f2fde393669024ac7a3c494a847cef0107fba 100644 (file)
@@ -565,12 +565,18 @@ SBuf::c_str()
 SBuf&
 SBuf::chop(size_type pos, size_type n)
 {
-    if (pos == npos || pos > length() || n == 0) {
+    if (pos == npos || pos > length())
+        pos = length();
+
+    if (n == npos || (pos+n) > length())
+        n = length() - pos;
+
+    // if there will be nothing left, reset the buffer while we can
+    if (pos == length() || n == 0) {
         clear();
         return *this;
     }
-    if (n == npos || (pos+n) > length())
-        n = length()-pos;
+
     ++stats.chop;
     off_ += pos;
     len_ = n;