Remove excessive memory allocation from dcb_printf

The function allocated a constant-sized chunk of memory for all messages
which was excessive as well as potentially dangerous when used with large
strings.
This commit is contained in:
Markus Mäkelä
2019-03-28 12:39:06 +02:00
parent c4b82b7d83
commit 0fa7ad8580

View File

@ -1767,20 +1767,24 @@ const char* gw_dcb_state2string(dcb_state_t state)
*/
void dcb_printf(DCB* dcb, const char* fmt, ...)
{
GWBUF* buf;
va_list args;
if ((buf = gwbuf_alloc(10240)) == NULL)
{
return;
}
va_start(args, fmt);
vsnprintf((char*)GWBUF_DATA(buf), 10240, fmt, args);
int n = vsnprintf(nullptr, 0, fmt, args);
va_end(args);
buf->end = (void*)((char*)GWBUF_DATA(buf) + strlen((char*)GWBUF_DATA(buf)));
GWBUF* buf = gwbuf_alloc(n + 1);
if (buf)
{
va_start(args, fmt);
vsnprintf((char*)GWBUF_DATA(buf), n + 1, fmt, args);
va_end(args);
// Remove the trailing null character
GWBUF_RTRIM(buf, 1);
dcb->func.write(dcb, buf);
}
}
/**
* Write data to a DCB socket through an SSL structure. The SSL structure is