summaryrefslogtreecommitdiff
path: root/sysdep
diff options
context:
space:
mode:
authorMaria Matejka <mq@ucw.cz>2019-07-10 11:12:41 +0200
committerMaria Matejka <mq@jmq.cz>2019-07-18 12:41:45 +0200
commit46faedff2990ca3e065931b36ab2133b3633bd25 (patch)
tree32c8d03b4c5287fcce6d224f16b098fb9fc8d134 /sysdep
parent9d23aa7a80d397f882cf60ff9b04f330b81dc1f0 (diff)
Debug: growing message format buffer
This led in corner cases to undefined buffer content and garbage output.
Diffstat (limited to 'sysdep')
-rw-r--r--sysdep/unix/log.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/sysdep/unix/log.c b/sysdep/unix/log.c
index 08623d01..22291dc1 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, " ... <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);