Remix.run Logo
▲ masklinn 2 hours ago

> If a compiler can detect UB

In the general case it can not, that is why they were made UBs in the first place.

And that is generally not how compilers see UB, they don’t look for UBs lying around, they assume UBs can’t happen and use that for constraints, which they then propagate.

For instance

    *i = 1
    if (id) { … }
The compiler will likely remove the check, because the dereference tags i as non-null, which makes the test redundant.

And this occurs and is extremely desirable every time e.g. a function is inlined in an other one which already checked for null.

▲nine_k an hour ago | parent [-]

But how is your example UB? It's straightforward data flow analysis.

AFAICT, UBI is stuff like that:

  short int i;
  for (i = 0; i < 33000; i++) do_something();
Whether this program continues as normal, crashes, or enters an infinite loop is platform-dependent, because short int can be as small as 16 bit, and an integer overflow past 32767 can be a crash or a wraparound.