logoalt Hacker News

jstimpfletoday at 12:35 PM2 repliesview on HN

You can run your code under ASAN and UBSAN nowadays, it will catch many or most of issues as they happen.

But that's completely besides the point. UB on signed overflow, or really most of UB, is not unrelated to C flexibility. It is a detail of the spec related to portability and performance. IIRC it is even required to make such trivial optimizations as turning

    for (int i = 0; i < n; i++) func(a[i]);
into

    for (Foo *p = a, *last = a + n; p < last; p++) func(p);
saving arithmetics and saving a register, on architectures where `int` is smaller than pointers. But there is also options like -fwrapv on GCC for example, allowing you to actually use signed overflow.

Replies

Chinjuttoday at 1:39 PM

How is undefined behavior necessary for this transformation?

show 1 reply
jstimpfletoday at 2:37 PM

*is not related to C flexibility