Skip to content

Commit

Permalink
Make sure this works properly under uncommon integer overflow rules
Browse files Browse the repository at this point in the history
While every tool I've ever heard of will implement this operation
correctly, it's technically signed overflow and thus undefined behavior.
Surprisingly, ubsan doesn't catch it...
  • Loading branch information
aaaaaa123456789 committed Jan 31, 2022
1 parent 223b896 commit 309edf0
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static inline uint64_t color_from_floats (double red, double green, double blue,

static inline int16_t make_signed_16 (uint16_t value) {
// this is a no-op (since int16_t must use two's complement), but it's necessary to avoid undefined behavior
return (value >= 0x8000u) ? -(int16_t) (~value) - 1 : value;
return (value >= 0x8000u) ? -(int16_t) bitnegate(value) - 1 : value;
}

static inline unsigned bit_width (uintmax_t value) {
Expand Down

0 comments on commit 309edf0

Please sign in to comment.