Debug: growing message format buffer
This led in corner cases to undefined buffer content and garbage output.
This commit is contained in:
parent
deb84d7989
commit
422a933429
1 changed files with 12 additions and 3 deletions
|
@ -309,14 +309,23 @@ die(const char *msg, ...)
|
||||||
void
|
void
|
||||||
debug(const char *msg, ...)
|
debug(const char *msg, ...)
|
||||||
{
|
{
|
||||||
|
#define MAX_DEBUG_BUFSIZE 65536
|
||||||
va_list args;
|
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);
|
va_start(args, msg);
|
||||||
if (dbgf)
|
if (dbgf)
|
||||||
{
|
{
|
||||||
if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
|
while (bvsnprintf(buf, bufsize, msg, args) < 0)
|
||||||
bsprintf(buf + sizeof(buf) - 100, " ... <too long>\n");
|
if (bufsize >= MAX_DEBUG_BUFSIZE)
|
||||||
|
bug("Extremely long debug output, split it.");
|
||||||
|
else
|
||||||
|
buf = mb_realloc(buf, (bufsize *= 2));
|
||||||
|
|
||||||
fputs(buf, dbgf);
|
fputs(buf, dbgf);
|
||||||
}
|
}
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
Loading…
Reference in a new issue