From: Todd C. Miller Date: Wed, 19 Oct 2016 18:56:34 +0000 (-0600) Subject: Use a static buffer if possible. X-Git-Tag: SUDO_1_8_19^2~99 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d078450fb57307506763d4a467df0187c5ccb8bc;p=sudo Use a static buffer if possible. --- diff --git a/lib/util/vsyslog.c b/lib/util/vsyslog.c index 2cc2b0b00..44f7bcebf 100644 --- a/lib/util/vsyslog.c +++ b/lib/util/vsyslog.c @@ -35,7 +35,8 @@ void sudo_vsyslog(int pri, const char *fmt, va_list ap) { int saved_errno = errno; - char *buf, *cp, *ep, new_fmt[1024]; + char *cp, *ep, msgbuf[8192], new_fmt[1024]; + va_list ap2; size_t len; /* Rewrite fmt, replacing %m with an errno string. */ @@ -59,10 +60,19 @@ sudo_vsyslog(int pri, const char *fmt, va_list ap) } *cp = '\0'; - /* Format message and log it. */ - if (vasprintf(&buf, new_fmt, ap) != -1) { - syslog(pri, "%s", buf); - free(buf); + /* Format message and log it, using a static buffer if possible. */ + va_copy(ap2, ap); + len = (size_t)snprintf(msgbuf, sizeof(msgbuf), new_fmt, ap2); + va_end(ap2); + if (len < sizeof(msgbuf)) { + syslog(pri, "%s", msgbuf); + } else { + /* Too big for static buffer? */ + char *buf; + if (vasprintf(&buf, new_fmt, ap) != -1) { + syslog(pri, "%s", buf); + free(buf); + } } } #endif /* HAVE_VSYSLOG */