From: Maria Matejka Date: Wed, 10 Jul 2019 09:12:41 +0000 (+0200) Subject: Debug: growing message format buffer X-Git-Tag: v1.6.7~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46faedff2990ca3e065931b36ab2133b3633bd25;p=thirdparty%2Fbird.git Debug: growing message format buffer This led in corner cases to undefined buffer content and garbage output. --- diff --git a/sysdep/unix/log.c b/sysdep/unix/log.c index 08623d01d..22291dc1e 100644 --- a/sysdep/unix/log.c +++ b/sysdep/unix/log.c @@ -242,14 +242,23 @@ die(const char *msg, ...) void debug(const char *msg, ...) { +#define MAX_DEBUG_BUFSIZE 65536 va_list args; - char buf[1024]; + static uint bufsize = 4096; + static char *buf = NULL; + + if (!buf) + buf = mb_alloc(&root_pool, bufsize); va_start(args, msg); if (dbgf) { - if (bvsnprintf(buf, sizeof(buf), msg, args) < 0) - bsprintf(buf + sizeof(buf) - 100, " ... \n"); + while (bvsnprintf(buf, bufsize, msg, args) < 0) + if (bufsize >= MAX_DEBUG_BUFSIZE) + bug("Extremely long debug output, split it."); + else + buf = mb_realloc(buf, (bufsize *= 2)); + fputs(buf, dbgf); } va_end(args);