Re: logger: correct snprintf usage

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

 



On Sat, Jul 28, 2007 at 10:41:37PM -0600, LaMont Jones wrote:
> 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.

Sigh... here is a properly formatted patch (that actually compiles and
is tested, too.)

lamont
>From df56f18c05b2a98d246c4588d997695db21a5cdb Mon Sep 17 00:00:00 2001
From: LaMont Jones <lamont@xxxxxxxxxxxx>
Date: Sat, 28 Jul 2007 23:03:52 -0600
Subject: [PATCH] logger: fix snprintf usage


Signed-off-by: LaMont Jones <lamont@xxxxxxxxxxxx>
---
 misc-utils/logger.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9a7cb05..49ba5dc 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -89,9 +89,10 @@ mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
        time_t now;
 
        if (fd > -1) {
-               if (logflags & LOG_PID)
-                       snprintf (pid, sizeof(pid), "[%d]", getpid());
-	       else
+               if (logflags & LOG_PID) {
+                       snprintf (pid, sizeof(pid)-1, "[%d]", getpid());
+		       pid[sizeof(pid)-1]=0;
+	       } else
 		       pid[0] = 0;
                if (tag)
 		       cp = 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 */
-- 
1.5.2.3


[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