logger: correct snprintf usage

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

 



snprintf doesn't write the trailing null to the buffer in the case of
truncation.  The following patch fixes logger.c the rest of the way.

lamont
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9a7cb05..40db42b 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -90,7 +90,8 @@ mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
 
        if (fd > -1) {
                if (logflags & LOG_PID)
-                       snprintf (pid, sizeof(pid), "[%d]", getpid());
+                       snprintf (pid, sizeof(pid)-1, "[%d]", getpid());
+		       pid[sizeof(pid)-1]=0;
 	       else
 		       pid[0] = 0;
                if (tag)
@@ -103,8 +104,9 @@ mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
                (void)time(&now);
 	       tp = ctime(&now)+4;
 
-               snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
+               snprintf(buf, sizeof(buf)-1, "<%d>%.15s %.200s%s: %.400s",
 			pri, tp, cp, pid, msg);
+	       buf[sizeof(buf)-1]=0;
 
                if (write(fd, buf, strlen(buf)+1) < 0)
                        return; /* error */
-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux