]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: tools: Fix loop condition in dump_text()
authorTim Duesterhus <tim@bastelstu.be>
Sat, 28 Aug 2021 22:58:22 +0000 (00:58 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 30 Aug 2021 04:14:50 +0000 (06:14 +0200)
The condition should first check whether `bsize` is reached, before
dereferencing the offset. Even if this always works fine, due to the
string being null-terminated, this certainly looks odd.

Found using GitHub's CodeQL scan.

This bug traces back to at least 97c2ae13bc0d7961a348102d6719fbcaf34d46d5
(1.7.0+) and this patch should be backported accordingly.

src/tools.c

index 4f536efc3ee4bafc22a58e08416b91096cac8cc4..cc71d45358af0f4cb94c7c2a23a26ad0d94b848a 100644 (file)
@@ -4522,9 +4522,9 @@ int may_access(const void *ptr)
 int dump_text(struct buffer *out, const char *buf, int bsize)
 {
        unsigned char c;
-       int ptr = 0;
+       size_t ptr = 0;
 
-       while (buf[ptr] && ptr < bsize) {
+       while (ptr < bsize && buf[ptr]) {
                c = buf[ptr];
                if (isprint((unsigned char)c) && isascii((unsigned char)c) && c != '\\' && c != ' ' && c != '=') {
                        if (out->data > out->size - 1)