Skip to content

Commit

Permalink
inet_cksum(): use unsigned types and change return type
Browse files Browse the repository at this point in the history
Fix Coverity Scan CID 469910 (Overflowed constant)

Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Oct 28, 2024
1 parent b47e381 commit d3d9d32
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions src/inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 */
}

/**
Expand Down

0 comments on commit d3d9d32

Please sign in to comment.