From f73fb9f8d0a0d8cfe0eb207e96a84ed1e4a60ffb Mon Sep 17 00:00:00 2001 From: "Barak A. Pearlmutter" Date: Wed, 2 Apr 2014 12:52:32 +0100 Subject: [PATCH] rewrite comparison to avoid negative unsigned numbers Note that GCC -O2 is happy to optimize away (x<0) when x is an unsigned quantity. This was actually occurring in CHECKLEN(0), causing the compiler to issue a warning. --- src/dns.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dns.c b/src/dns.c index fd3ce25..bd1257f 100644 --- a/src/dns.c +++ b/src/dns.c @@ -45,7 +45,7 @@ int dnsc_use_edns0 = 1; -#define CHECKLEN(x) if (buflen - (p-buf) < (x)) return 0 +#define CHECKLEN(x) if (buflen < (x) + (unsigned)(p-buf)) return 0 int dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, char *data, size_t datalen) @@ -391,7 +391,7 @@ dns_get_id(char *packet, size_t packetlen) return ntohs(header->id); } -#define CHECKLEN(x) if (packetlen - (data-packet) < (x)) return 0 +#define CHECKLEN(x) if (packetlen < (x) + (unsigned)(data-packet)) return 0 int dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, size_t packetlen)