]> The Tcpdump Group git mirrors - tcpdump/blobdiff - util.c
add a maxlen boundary check to safeputs, print unprintable chars as hex in safeputchar
[tcpdump] / util.c
diff --git a/util.c b/util.c
index 13d299749ac8e380bd16179e0b9698269a5c791c..cb4f2aaa768ce16a5fd07261f14c6d0ee4b1e9cd 100644 (file)
--- a/util.c
+++ b/util.c
@@ -21,7 +21,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.103 2005-12-13 08:37:23 hannes Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.106 2006-02-08 01:38:16 hannes Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -164,6 +164,8 @@ ts_print(register const struct timeval *tvp)
        time_t Time;
        static unsigned b_sec;
        static unsigned b_usec;
+       int d_usec;
+       int d_sec;
 
        switch (tflag) {
 
@@ -183,14 +185,14 @@ ts_print(register const struct timeval *tvp)
 
        case 3: /* Microseconds since previous packet */
         case 5: /* Microseconds since first packet */
-               if (b_sec == 0 && tflag == 5) {
+               if (b_sec == 0) {
                         /* init timestamp for first packet */
                         b_usec = tvp->tv_usec;
                         b_sec = tvp->tv_sec;                        
                 }
 
-                int d_usec = tvp->tv_usec - b_usec;
-                int d_sec = tvp->tv_sec - b_sec;
+                d_usec = tvp->tv_usec - b_usec;
+                d_sec = tvp->tv_sec - b_sec;
                 
                 while (d_usec < 0) {
                     d_usec += 1000000;
@@ -524,10 +526,12 @@ read_infile(char *fname)
 }
 
 void
-safeputs(const char *s)
+safeputs(const char *s, int maxlen)
 {
-       while (*s) {
+    int idx = 0;
+       while (*s && idx < maxlen) {
                safeputchar(*s);
+                idx++;
                s++;
        }
 }
@@ -541,5 +545,5 @@ safeputchar(int c)
        if (ch < 0x80 && isprint(ch))
                printf("%c", ch);
        else
-               printf("\\%03o", ch);
+               printf("\\0x%02x ", ch);
 }