]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Debug: growing message format buffer
authorMaria Matejka <mq@ucw.cz>
Wed, 10 Jul 2019 09:12:41 +0000 (11:12 +0200)
committerMaria Matejka <mq@jmq.cz>
Thu, 18 Jul 2019 10:41:45 +0000 (12:41 +0200)
This led in corner cases to undefined buffer content
and garbage output.

sysdep/unix/log.c

index 08623d01d11306f1cead20136e594034a256f1bf..22291dc1e6e8d7555089f68cd93ac9fc3ba2480f 100644 (file)
@@ -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, " ... <too long>\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);