Idiom exclusion is really so important
Idiom exclusion is really so important
Posted Nov 13, 2024 10:54 UTC (Wed) by adobriyan (subscriber, #30858)In reply to: Idiom exclusion is really so important by pm215
Parent article: Progress on toolchain security features
and passing the result is cumbersome or not necessary.
The _right_ way is to not invent new idioms (which every project will do differently) but to use __builtin_add_overflow().
T len;
if (__builtin_add_overflow(a, b, &len)) { return -E; };
[use len]
or (because clang doesn't do BAO_p)
if (__builtin_add_overflow(a, b, &(int[1]){})) { return -E; };
Those who fear that BAO does addition differently and will make people break glass and cut hands may want to lobby for a warning if BAO is done on different types (especially with both different signedness _and_ length).
BAO is cool for checking if result fits into some other type:
if (__builtin_add_overflow(a, 0, &(T[1]){})
Just use BAO.
