]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: buffers: Fix bi/bo_contig_space to handle full buffers
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 13 Jun 2017 20:00:22 +0000 (22:00 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 14 Jun 2017 14:20:20 +0000 (16:20 +0200)
These functions was added in commit 637f8f2c ("BUG/MEDIUM: buffers: Fix how
input/output data are injected into buffers").

This patch fixes hidden bugs. When a buffer is full (buf->i + buf->o ==
buf->size), instead of returning 0, these functions can return buf->size. Today,
this never happens because callers already check if the buffer is full before
calling bi/bo_contig_space. But to avoid possible bugs if calling conditions
changed, we slightly refactored these functions.

include/common/buffer.h

index 040a26e2f08ea9c8a8780b78c88eba3917c7a3c1..f8bd26efbb4c07d2c9aa3ae3e01b271455dc7e64 100644 (file)
@@ -163,12 +163,16 @@ static inline int bi_contig_space(const struct buffer *b)
 {
        const char *left, *right;
 
-       left  = bi_end(b);
-       right = bo_ptr(b);
-
-       if (left >= right)
-               right = b->data + b->size;
-
+       left  = b->p + b->i;
+       right = b->p - b->o;
+       if (left >= b->data + b->size)
+               left -= b->size;
+       else {
+               if (right < b->data)
+                       right += b->size;
+               else
+                       right = b->data + b->size;
+       }
        return (right - left);
 }
 
@@ -181,10 +185,11 @@ static inline int bo_contig_space(const struct buffer *b)
 {
        const char *left, *right;
 
-       left  = bo_end(b);
-       right = bo_ptr(b);
-
-       if (left >= right)
+       left  = b->p;
+       right = b->p - b->o;
+       if (right < b->data)
+               right += b->size;
+       else
                right = b->data + b->size;
 
        return (right - left);