cmdline (merge-intel): don't limit output len of cmdline_printf()
authorOlivier Matz <zer0@droids-corp.org>
Fri, 24 Dec 2010 12:56:06 +0000 (13:56 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Sun, 2 Jan 2011 20:53:15 +0000 (21:53 +0100)
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>
src/lib/cmdline.c

index bafaace..6d37885 100644 (file)
@@ -149,14 +149,33 @@ void
 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