From 30b028f9da2948a88207da7ae4c1d34e7c0a0f09 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Sat, 18 Apr 2015 19:56:49 -0700 Subject: [PATCH] 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. --- netdissect.h | 7 +++---- util-print.c | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/netdissect.h b/netdissect.h index f4de98cd..be78857e 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-print.c b/util-print.c index 4909c64a..2c3eb659 100644 --- a/util-print.c +++ b/util-print.c @@ -338,7 +338,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; @@ -356,12 +356,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) { @@ -376,7 +376,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 */ @@ -386,7 +386,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); } @@ -396,9 +396,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, "")); } /* @@ -407,9 +407,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, ", ")); } /* -- 2.39.5