Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libwrap Format the diagnostic with vasprintf once and us...



details:   https://anonhg.NetBSD.org/src/rev/79ca00e94e45
branches:  trunk
changeset: 778350:79ca00e94e45
user:      joerg <joerg%NetBSD.org@localhost>
date:      Thu Mar 22 22:58:15 2012 +0000

description:
Format the diagnostic with vasprintf once and use plain syslog instead
of messing with format strings.

diffstat:

 lib/libwrap/diag.c |  44 ++++++++++++++++----------------------------
 1 files changed, 16 insertions(+), 28 deletions(-)

diffs (86 lines):

diff -r e7174c64c254 -r 79ca00e94e45 lib/libwrap/diag.c
--- a/lib/libwrap/diag.c        Thu Mar 22 22:48:56 2012 +0000
+++ b/lib/libwrap/diag.c        Thu Mar 22 22:58:15 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: diag.c,v 1.9 2012/03/21 10:10:37 matt Exp $    */
+/*     $NetBSD: diag.c,v 1.10 2012/03/22 22:58:15 joerg Exp $  */
 
  /*
   * Routines to report various classes of problems. Each report is decorated
@@ -16,7 +16,7 @@
 #if 0
 static char sccsid[] = "@(#) diag.c 1.1 94/12/28 17:42:20";
 #else
-__RCSID("$NetBSD: diag.c,v 1.9 2012/03/21 10:10:37 matt Exp $");
+__RCSID("$NetBSD: diag.c,v 1.10 2012/03/22 22:58:15 joerg Exp $");
 #endif
 #endif
 
@@ -25,6 +25,7 @@
 #include <syslog.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <setjmp.h>
 #include <string.h>
 #include <errno.h>
@@ -37,46 +38,33 @@
 jmp_buf tcpd_buf;
 
 static void tcpd_diag(int, const char *, const char *, va_list)
-       __attribute__((__format__(__printf__, 3, 0)));
+    __printflike(3,0);
 
 /* tcpd_diag - centralize error reporter */
 
 static void
-tcpd_diag(int severity, const char *tag, const char *format, va_list ap)
+tcpd_diag(int severity, const char *tag, const char *fmt, va_list ap)
 {
-    char    fmt[BUFSIZ];
-    char    buf[BUFSIZ];
-    size_t  i, o;
+    char *buf;
     int     oerrno;
 
     /* save errno in case we need it */
     oerrno = errno;
 
+    if (vasprintf(&buf, fmt, ap) == -1)
+       buf = __UNCONST(fmt);
+
+    errno = oerrno;
+
     /* contruct the tag for the log entry */
     if (tcpd_context.file)
-       (void)snprintf(buf, sizeof buf, "%s: %s, line %d: ",
-               tag, tcpd_context.file, tcpd_context.line);
+       syslog(severity, "%s: %s, line %d: %s",
+           tag, tcpd_context.file, tcpd_context.line, buf);
     else
-       (void)snprintf(buf, sizeof buf, "%s: ", tag);
+       syslog(severity, "%s: %s", tag, buf);
 
-    /* change % to %% in tag before appending the format */
-    for (i = 0, o = 0; buf[i] != '\0'; ) {
-       if (buf[i] == '%') {
-           fmt[o] = '%';
-           if (o < sizeof(fmt) - 1)
-               o++;
-       }
-       fmt[o] = buf[i++];
-       if (o < sizeof(fmt) - 1)
-           o++;
-    }
-
-    /* append format and force null termination */
-    fmt[o] = '\0';
-    (void)strlcat(fmt, format, sizeof(fmt) - o);
-
-    errno = oerrno;
-    vsyslog(severity, fmt, ap);
+    if (buf != fmt)
+        free(buf);
 }
 
 /* tcpd_warn - report problem of some sort and proceed */



Home | Main Index | Thread Index | Old Index