Skip to content

Commit

Permalink
Address integer overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
fenner committed Oct 12, 2022
1 parent e4b2fd1 commit 6d0b77d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ create_osi_cksum (const uint8_t *pptr, int checksum_offset, int length)

int x;
int y;
uint32_t mul;
int32_t mul;
uint32_t c0;
uint32_t c1;
uint64_t c1;
uint16_t checksum;
int idx;

Expand All @@ -169,8 +169,14 @@ create_osi_cksum (const uint8_t *pptr, int checksum_offset, int length)

mul = (length - checksum_offset)*(c0);

x = mul - c0 - c1;
y = c1 - mul;
/*
* Casting c0 and c1 here is guaranteed to be safe, because we know
* they have values between 0 and 254 inclusive. These casts are
* done to ensure that all of the arithmetic operations are
* well-defined (i.e., not mixing signed and unsigned integers).
*/
x = mul - (int)c0 - (int)c1;
y = (int)c1 - mul;

x %= 255;
y %= 255;
Expand Down

0 comments on commit 6d0b77d

Please sign in to comment.