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:
@ -1767,19 +1767,23 @@ const char* gw_dcb_state2string(dcb_state_t state)
|
|||||||
*/
|
*/
|
||||||
void dcb_printf(DCB* dcb, const char* fmt, ...)
|
void dcb_printf(DCB* dcb, const char* fmt, ...)
|
||||||
{
|
{
|
||||||
GWBUF* buf;
|
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
if ((buf = gwbuf_alloc(10240)) == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
vsnprintf((char*)GWBUF_DATA(buf), 10240, fmt, args);
|
int n = vsnprintf(nullptr, 0, fmt, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
buf->end = (void*)((char*)GWBUF_DATA(buf) + strlen((char*)GWBUF_DATA(buf)));
|
GWBUF* buf = gwbuf_alloc(n + 1);
|
||||||
dcb->func.write(dcb, buf);
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user