From d3d9d32ebec28b0a7789846e3694f0037bad945d Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sun, 27 Oct 2024 16:22:15 +0100 Subject: [PATCH] inet_cksum(): use unsigned types and change return type Fix Coverity Scan CID 469910 (Overflowed constant) Signed-off-by: Joachim Wiberg --- src/defs.h | 2 +- src/inet.c | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/defs.h b/src/defs.h index 1bc07f1..62468f6 100644 --- a/src/defs.h +++ b/src/defs.h @@ -358,7 +358,7 @@ extern char *inet_name(uint32_t, int); extern char *inet_fmt(uint32_t, char *, size_t); extern char *inet_fmts(uint32_t, uint32_t, char *, size_t); extern uint32_t inet_parse(char *, int); -extern int inet_cksum(uint16_t *, uint32_t); +extern uint16_t inet_cksum(uint16_t *, uint32_t); /* prune.c */ extern struct gtable *kernel_table; diff --git a/src/inet.c b/src/inet.c index eb325f1..86efde3 100644 --- a/src/inet.c +++ b/src/inet.c @@ -225,12 +225,12 @@ uint32_t inet_parse(char *s, int n) * Checksum routine for Internet Protocol family headers (C Version) * */ -int inet_cksum(uint16_t *addr, uint32_t len) +uint16_t inet_cksum(uint16_t *addr, uint32_t len) { int nleft = (int)len; uint16_t *w = addr; uint16_t answer = 0; - int32_t sum = 0; + uint32_t sum = 0; /* * Our algorithm is simple, using a 32 bit accumulator (sum), @@ -254,9 +254,7 @@ int inet_cksum(uint16_t *addr, uint32_t len) */ sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ sum += (sum >> 16); /* add carry */ - answer = ~sum; /* truncate to 16 bits */ - - return answer; + return (uint16_t)~sum; /* truncate to 16 bits */ } /**