]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Use unsigned values in tok2str and bittok2str routines.
authorGuy Harris <[email protected]>
Sun, 19 Apr 2015 02:58:16 +0000 (19:58 -0700)
committerGuy Harris <[email protected]>
Sun, 19 Apr 2015 02:58:16 +0000 (19:58 -0700)
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
util.c

index 818f8eca2536e407675690cef0d11df9d186f4d1..d507f580aa125d3f81e7c450594afac833fc6a2d 100644 (file)
@@ -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 5569021c242ca7d9155f4bcdb68a19353cc6ea9b..b37f3d819542f01d41c4adb7a1637859c15b8a69 100644 (file)
--- 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, ", "));
 }
 
 /*