]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: chunk: fix chunk_appendf() to not write a zero if buffer is full
authorDragan Dosen <ddosen@haproxy.com>
Thu, 27 Jul 2023 18:30:42 +0000 (20:30 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 27 Jul 2023 20:05:25 +0000 (22:05 +0200)
If the buffer is completely full, the function chunk_appendf() would
write a zero past it, which can result in unexpected behavior.

Now we make a check before calling vsnprintf() and return the current
chunk size if no room is available.

This should be backported as far as 2.0.

src/chunk.c

index 2d24fa596d36a4018c118e6d72a1ff7c107fa7e5..b9728e1c9110361cc86fdf2126b88654471186cf 100644 (file)
@@ -130,15 +130,19 @@ int chunk_printf(struct buffer *chk, const char *fmt, ...)
 int chunk_appendf(struct buffer *chk, const char *fmt, ...)
 {
        va_list argp;
+       size_t room;
        int ret;
 
        if (!chk->area || !chk->size)
                return 0;
 
+       room = chk->size - chk->data;
+       if (!room)
+               return chk->data;
+
        va_start(argp, fmt);
-       ret = vsnprintf(chk->area + chk->data, chk->size - chk->data, fmt,
-                       argp);
-       if (ret >= chk->size - chk->data)
+       ret = vsnprintf(chk->area + chk->data, room, fmt, argp);
+       if (ret >= room)
                /* do not copy anything in case of truncation */
                chk->area[chk->data] = 0;
        else