From 2d96be0322d71ce62e2f68d038070b10a7c9201b Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Mon, 25 Jul 2016 16:05:27 -0700 Subject: Check for, and squelch, signed vs. unsigned comparison errors. Use -Wsign-compare if the compiler supports it. If we're comparing an unsigned value with a signed value, and we've already determined that the signed value is >= 0, just cast it to an unsigned type. Use 0xffffffffU instead of -1 as the "unknown"/"unset"/etc. value for unsigned variables and fields. Assign the result of str2tok() to an int, not a u_int, as it can return -1. Declare some variables and fields unsigned if they don't need to be signed. Make sure some arguments passed into "set" functions are non-negative (and otherwise not invalid). In the BPF optimizer, cast the "constant" fields of a struct block to an unsigned type - the actual constant field of a BPF instruction is unsigned. Also, cast away one warning from MSVC. --- optimize.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'optimize.c') diff --git a/optimize.c b/optimize.c index d0a90683..baa9427c 100644 --- a/optimize.c +++ b/optimize.c @@ -885,7 +885,7 @@ opt_peep(opt_state_t *opt_state, struct block *b) if (b->s.code == (BPF_JMP|BPF_K|BPF_JSET)) { if (b->s.k == 0) JT(b) = JF(b); - if (b->s.k == 0xffffffff) + if ((u_int)b->s.k == 0xffffffffU) JF(b) = JT(b); } /* @@ -913,11 +913,11 @@ opt_peep(opt_state_t *opt_state, struct block *b) break; case BPF_JGT: - v = (unsigned)v > b->s.k; + v = (unsigned)v > (unsigned)b->s.k; break; case BPF_JGE: - v = (unsigned)v >= b->s.k; + v = (unsigned)v >= (unsigned)b->s.k; break; case BPF_JSET: @@ -2046,7 +2046,7 @@ convert_code_r(compiler_state_t *cstate, conv_state_t *conv_state, dst = conv_state->ftail -= (slen + 1 + p->longjt + p->longjf); /* inflate length by any extra jumps */ - p->offset = dst - conv_state->fstart; + p->offset = (int)(dst - conv_state->fstart); /* generate offset[] for convenience */ if (slen) { -- cgit v1.2.3