Re:Porting 4.5K lines of C to Go
by asveikau on Saturday August 05, 2017 @12:27PM
(#14936473)
I have been writing C for a while and never saw anyone add bools with +. Why do this when there is completely unambiguous || ?
I get suspicious of being fast and loose with conversions between int and bool. This is implicitly required when working with operators like &&, ||, ==, etc., and working with pre-C99 bools and library typedefs. But assigning anything other than 1 or 0 is asking for problems. There is a whole class of bugs that shows up in security bugs involving assigning someone's machine-word-sized Boolean expression into someone else's byte sized bool, losing bits, and flipping from true to false.
I sometimes feel there is a ^^ operator that should be there and is missing, in the same way that we have && and || corresponding to & and |. I have occasionally faked it with a macro:
#define XOR(A,B) (((A) ? 1 : 0) ^ ((B) ? 1 : 0))
The ? here eliminates the problems from using larger != 1 quantities as true, as above.
I get suspicious of being fast and loose with conversions between int and bool. This is implicitly required when working with operators like &&, ||, ==, etc., and working with pre-C99 bools and library typedefs. But assigning anything other than 1 or 0 is asking for problems. There is a whole class of bugs that shows up in security bugs involving assigning someone's machine-word-sized Boolean expression into someone else's byte sized bool, losing bits, and flipping from true to false.
I sometimes feel there is a ^^ operator that should be there and is missing, in the same way that we have && and || corresponding to & and |. I have occasionally faked it with a macro:
The ? here eliminates the problems from using larger != 1 quantities as true, as above.