Skip to content

Commit

Permalink
Address integer overflows
Browse files Browse the repository at this point in the history
Unlike some checksum routines that use the defined semantics of
2's-complement unsigned overflow to their advantage, this one
gets the wrong value if it is allowed to overflow, due to the
use of mod-255.

Convert c1 to uint64_t to avoid overflow

checksum.c:163:16: runtime error: unsigned integer overflow: NNN + NNN cannot be represented in type 'unsigned int'

Use integers during subtraction to avoid implicit conversion to unsigned
when calculating both x and y

checksum.c:172:18: runtime error: unsigned integer overflow: NNN - NNN cannot be represented in type 'unsigned int'
checksum.c:172:9: runtime error: implicit conversion from type 'unsigned int' of value NNN (32-bit, unsigned) to type 'int' changed the value to -NNN (32-bit, signed)
checksum.c:173:12: runtime error: unsigned integer overflow: NNN - NNN cannot be represented in type 'unsigned int'
checksum.c:173:9: runtime error: implicit conversion from type 'unsigned int' of value NNN (32-bit, unsigned) to type 'int' changed the value to -NNN (32-bit, signed)
  • Loading branch information
fenner committed Oct 13, 2022
1 parent 73044c2 commit 0445332
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 0445332

Please sign in to comment.