From: guy Date: Sun, 28 May 2006 20:13:42 +0000 (+0000) Subject: C89 says that the result of shifting an N-bit value by >= N bits is X-Git-Tag: libpcap-0.9.5~12 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/8bdc90b418fcd4d55df3c6f59a1f9e4db86f62c2 C89 says that the result of shifting an N-bit value by >= N bits is undefined; we want it to be zero, so we explicitly check for a 32-bit shift count and clear the netmask in that case. --- diff --git a/gencode.c b/gencode.c index d39330c9..d1c039db 100644 --- a/gencode.c +++ b/gencode.c @@ -21,7 +21,7 @@ */ #ifndef lint static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.221.2.38 2006-02-22 10:39:49 hannes Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.221.2.39 2006-05-28 20:13:42 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -5011,7 +5011,14 @@ gen_mcode(s1, s2, masklen, q) /* Convert mask len to mask */ if (masklen > 32) bpf_error("mask length must be <= 32"); - m = 0xffffffff << (32 - masklen); + if (masklen == 0) { + /* + * X << 32 is not guaranteed by C to be 0; it's + * undefined. + */ + m = 0; + } else + m = 0xffffffff << (32 - masklen); if ((n & ~m) != 0) bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);