From: Francois-Xavier Le Bail Date: Tue, 30 Jun 2015 18:44:14 +0000 (+0200) Subject: Fix a non-reentrant code in a function X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/b2cb8112f92037838e8d4c1a1e2ef4a96c7793c9 Fix a non-reentrant code in a function 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. --- diff --git a/util.c b/util.c index b37f3d81..8fcf3e0c 100644 --- a/util.c +++ b/util.c @@ -54,6 +54,12 @@ #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; } }