Skip to content

Commit 8f01704

Browse files
committed
Use unsigned values in tok2str and bittok2str routines.
This prevents the compiler issue mentioned in GitHub issue the-tcpdump-group#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.
1 parent 685c8f4 commit 8f01704

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

netdissect.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ extern const char *tok2strbuf(const struct tok *, const char *, u_int,
8181
char *buf, size_t bufsize);
8282

8383
/* tok2str is deprecated */
84-
extern const char *tok2str(const struct tok *, const char *, int);
85-
extern char *bittok2str(const struct tok *, const char *, int);
86-
extern char *bittok2str_nosep(const struct tok *, const char *, int);
84+
extern const char *tok2str(const struct tok *, const char *, u_int);
85+
extern char *bittok2str(const struct tok *, const char *, u_int);
86+
extern char *bittok2str_nosep(const struct tok *, const char *, u_int);
8787

8888

8989
typedef struct netdissect_options netdissect_options;
@@ -293,7 +293,6 @@ extern void relts_print(netdissect_options *, int);
293293
extern int fn_print(netdissect_options *, const u_char *, const u_char *);
294294
extern int fn_printn(netdissect_options *, const u_char *, u_int, const u_char *);
295295
extern int fn_printzp(netdissect_options *, const u_char *, u_int, const u_char *);
296-
extern const char *tok2str(const struct tok *, const char *, int);
297296

298297
/*
299298
* Flags for txtproto_print().

util.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ tok2strbuf(register const struct tok *lp, register const char *fmt,
337337
*/
338338
const char *
339339
tok2str(register const struct tok *lp, register const char *fmt,
340-
register int v)
340+
register u_int v)
341341
{
342342
static char buf[4][128];
343343
static int idx = 0;
@@ -355,12 +355,12 @@ tok2str(register const struct tok *lp, register const char *fmt,
355355
*/
356356
static char *
357357
bittok2str_internal(register const struct tok *lp, register const char *fmt,
358-
register int v, register int sep)
358+
register u_int v, const char *sep)
359359
{
360360
static char buf[256]; /* our stringbuffer */
361361
int buflen=0;
362-
register int rotbit; /* this is the bit we rotate through all bitpositions */
363-
register int tokval;
362+
register u_int rotbit; /* this is the bit we rotate through all bitpositions */
363+
register u_int tokval;
364364
const char * sepstr = "";
365365

366366
while (lp != NULL && lp->s != NULL) {
@@ -375,7 +375,7 @@ bittok2str_internal(register const struct tok *lp, register const char *fmt,
375375
/* ok we have found something */
376376
buflen+=snprintf(buf+buflen, sizeof(buf)-buflen, "%s%s",
377377
sepstr, lp->s);
378-
sepstr = sep ? ", " : "";
378+
sepstr = sep;
379379
break;
380380
}
381381
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,
385385

386386
if (buflen == 0)
387387
/* bummer - lets print the "unknown" message as advised in the fmt string if we got one */
388-
(void)snprintf(buf, sizeof(buf), fmt == NULL ? "#%d" : fmt, v);
388+
(void)snprintf(buf, sizeof(buf), fmt == NULL ? "#%08x" : fmt, v);
389389
return (buf);
390390
}
391391

@@ -395,9 +395,9 @@ bittok2str_internal(register const struct tok *lp, register const char *fmt,
395395
*/
396396
char *
397397
bittok2str_nosep(register const struct tok *lp, register const char *fmt,
398-
register int v)
398+
register u_int v)
399399
{
400-
return (bittok2str_internal(lp, fmt, v, 0));
400+
return (bittok2str_internal(lp, fmt, v, ""));
401401
}
402402

403403
/*
@@ -406,9 +406,9 @@ bittok2str_nosep(register const struct tok *lp, register const char *fmt,
406406
*/
407407
char *
408408
bittok2str(register const struct tok *lp, register const char *fmt,
409-
register int v)
409+
register u_int v)
410410
{
411-
return (bittok2str_internal(lp, fmt, v, 1));
411+
return (bittok2str_internal(lp, fmt, v, ", "));
412412
}
413413

414414
/*

0 commit comments

Comments
 (0)