It was using a big buffer in stack. Now use vdprintf() if _GNU_SOURCE
is defined, or a malloc + free if not.
Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
{
va_list ap;
- char buffer[BUFSIZ];
+#ifdef _GNU_SOURCE
+ if (cl->s_out < 0)
+ return;
va_start(ap, fmt);
- vsnprintf(buffer, 512, fmt, ap);
+ vdprintf(cl->s_out, fmt, ap);
va_end(ap);
- if (cl->s_out >= 0) {
- write(cl->s_out, buffer, strlen(buffer));
- }
+#else
+ int ret;
+ char *buf;
+
+ if (cl->s_out < 0)
+ return;
+
+ buf = malloc(BUFSIZ);
+ if (buf == NULL)
+ return;
+ va_start(ap, fmt);
+ ret = vsnprintf(buf, BUFSIZ, fmt, ap);
+ va_end(ap);
+ if (ret < 0)
+ return;
+ if (ret >= BUFSIZ)
+ ret = BUFSIZ - 1;
+ write(cl->s_out, buf, ret);
+ free(buf);
+#endif
}
int