]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Fix a non-reentrant code in a function
authorFrancois-Xavier Le Bail <[email protected]>
Tue, 30 Jun 2015 18:44:14 +0000 (20:44 +0200)
committerFrancois-Xavier Le Bail <[email protected]>
Tue, 30 Jun 2015 18:50:09 +0000 (20:50 +0200)
Delete the 'static char buf' array in ts_format().
buf is now a parameter of ts_format().
Moreover:
Decrease a too long string.
Fix a space in a comment.

util.c

diff --git a/util.c b/util.c
index b37f3d819542f01d41c4adb7a1637859c15b8a69..8fcf3e0cfc97113a4498e9e67735a379c4bc8518 100644 (file)
--- a/util.c
+++ b/util.c
 
 #include "interface.h"
 
+/*
+ * timestamp display buffer size, the biggest size of both formats is needed
+ * sizeof("0000000000.000000000") > sizeof("00:00:00.000000000")
+ */
+#define TS_BUF_SIZE sizeof("0000000000.000000000")
+
 /*
  * Print out a null-terminated filename (or other ascii string).
  * If ep is NULL, assume no truncation check is needed.
@@ -155,9 +161,8 @@ ts_format(netdissect_options *ndo
 #ifndef HAVE_PCAP_SET_TSTAMP_PRECISION
 _U_
 #endif
-, int sec, int usec)
+, int sec, int usec, char *buf)
 {
-       static char buf[sizeof("00:00:00.000000000")];
        const char *format;
 
 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
@@ -172,14 +177,14 @@ _U_
                break;
 
        default:
-               format = "%02d:%02d:%02d.{unknown precision}";
+               format = "%02d:%02d:%02d.{unknown}";
                break;
        }
 #else
        format = "%02d:%02d:%02d.%06u";
 #endif
 
-       snprintf(buf, sizeof(buf), format,
+       snprintf(buf, TS_BUF_SIZE, format,
                  sec / 3600, (sec % 3600) / 60, sec % 60, usec);
 
         return buf;
@@ -199,12 +204,13 @@ ts_print(netdissect_options *ndo,
        static unsigned b_usec;
        int d_usec;
        int d_sec;
+       char buf[TS_BUF_SIZE];
 
        switch (ndo->ndo_tflag) {
 
        case 0: /* Default */
                s = (tvp->tv_sec + thiszone) % 86400;
-               ND_PRINT((ndo, "%s ", ts_format(ndo, s, tvp->tv_usec)));
+               ND_PRINT((ndo, "%s ", ts_format(ndo, s, tvp->tv_usec, buf)));
                break;
 
        case 1: /* No time stamp */
@@ -232,7 +238,7 @@ ts_print(netdissect_options *ndo,
                     d_sec--;
                 }
 
-                ND_PRINT((ndo, "%s ", ts_format(ndo, d_sec, d_usec)));
+                ND_PRINT((ndo, "%s ", ts_format(ndo, d_sec, d_usec, buf)));
 
                 if (ndo->ndo_tflag == 3) { /* set timestamp for last packet */
                     b_sec = tvp->tv_sec;
@@ -240,7 +246,7 @@ ts_print(netdissect_options *ndo,
                 }
                break;
 
-       case 4: /* Default + Date*/
+       case 4: /* Default + Date */
                s = (tvp->tv_sec + thiszone) % 86400;
                Time = (tvp->tv_sec + thiszone) - s;
                tm = gmtime (&Time);
@@ -249,7 +255,7 @@ ts_print(netdissect_options *ndo,
                else
                        ND_PRINT((ndo, "%04d-%02d-%02d %s ",
                                tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
-                               ts_format(ndo, s, tvp->tv_usec)));
+                               ts_format(ndo, s, tvp->tv_usec, buf)));
                break;
        }
 }