From: Guy Harris Date: Sun, 19 Apr 2015 02:58:16 +0000 (-0700) Subject: Use unsigned values in tok2str and bittok2str routines. X-Git-Tag: tcpdump-4.7.4^2 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/8f0170452f5ebc2aae97544f296e8984e942a5cb Use unsigned values in tok2str and bittok2str routines. This prevents the compiler issue mentioned in GitHub issue #451, and also cleans up some other signed vs. unsigned stuff. While we're at it, clean up bittok2str_internal() (just pass it the separator string, not a Boolean value that's tested to choose the separator string), and print unknown arguments to the bittok2str routines in hex, not decimal. --- diff --git a/netdissect.h b/netdissect.h index 818f8eca..d507f580 100644 --- a/netdissect.h +++ b/netdissect.h @@ -81,9 +81,9 @@ extern const char *tok2strbuf(const struct tok *, const char *, u_int, char *buf, size_t bufsize); /* tok2str is deprecated */ -extern const char *tok2str(const struct tok *, const char *, int); -extern char *bittok2str(const struct tok *, const char *, int); -extern char *bittok2str_nosep(const struct tok *, const char *, int); +extern const char *tok2str(const struct tok *, const char *, u_int); +extern char *bittok2str(const struct tok *, const char *, u_int); +extern char *bittok2str_nosep(const struct tok *, const char *, u_int); typedef struct netdissect_options netdissect_options; @@ -293,7 +293,6 @@ extern void relts_print(netdissect_options *, int); extern int fn_print(netdissect_options *, const u_char *, const u_char *); extern int fn_printn(netdissect_options *, const u_char *, u_int, const u_char *); extern int fn_printzp(netdissect_options *, const u_char *, u_int, const u_char *); -extern const char *tok2str(const struct tok *, const char *, int); /* * Flags for txtproto_print(). diff --git a/util.c b/util.c index 5569021c..b37f3d81 100644 --- a/util.c +++ b/util.c @@ -337,7 +337,7 @@ tok2strbuf(register const struct tok *lp, register const char *fmt, */ const char * tok2str(register const struct tok *lp, register const char *fmt, - register int v) + register u_int v) { static char buf[4][128]; static int idx = 0; @@ -355,12 +355,12 @@ tok2str(register const struct tok *lp, register const char *fmt, */ static char * bittok2str_internal(register const struct tok *lp, register const char *fmt, - register int v, register int sep) + register u_int v, const char *sep) { static char buf[256]; /* our stringbuffer */ int buflen=0; - register int rotbit; /* this is the bit we rotate through all bitpositions */ - register int tokval; + register u_int rotbit; /* this is the bit we rotate through all bitpositions */ + register u_int tokval; const char * sepstr = ""; while (lp != NULL && lp->s != NULL) { @@ -375,7 +375,7 @@ bittok2str_internal(register const struct tok *lp, register const char *fmt, /* ok we have found something */ buflen+=snprintf(buf+buflen, sizeof(buf)-buflen, "%s%s", sepstr, lp->s); - sepstr = sep ? ", " : ""; + sepstr = sep; break; } rotbit=rotbit<<1; /* no match - lets shift and try again */ @@ -385,7 +385,7 @@ bittok2str_internal(register const struct tok *lp, register const char *fmt, if (buflen == 0) /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */ - (void)snprintf(buf, sizeof(buf), fmt == NULL ? "#%d" : fmt, v); + (void)snprintf(buf, sizeof(buf), fmt == NULL ? "#%08x" : fmt, v); return (buf); } @@ -395,9 +395,9 @@ bittok2str_internal(register const struct tok *lp, register const char *fmt, */ char * bittok2str_nosep(register const struct tok *lp, register const char *fmt, - register int v) + register u_int v) { - return (bittok2str_internal(lp, fmt, v, 0)); + return (bittok2str_internal(lp, fmt, v, "")); } /* @@ -406,9 +406,9 @@ bittok2str_nosep(register const struct tok *lp, register const char *fmt, */ char * bittok2str(register const struct tok *lp, register const char *fmt, - register int v) + register u_int v) { - return (bittok2str_internal(lp, fmt, v, 1)); + return (bittok2str_internal(lp, fmt, v, ", ")); } /*