News for hackers. Stuff that matters.
Re:Falsehoods programmers believe about undefined behavior by bjornsing on Monday November 28, 2022 @07:13AM (#33772643)
> 14. Okay, but if the line with UB is unreachable (dead) code, then it's as if the UB wasn't there.

This one came as a surprise to me. Is this really (unconditionally) true? And if so, why? Seems trivial to me to ensure that UB that will never under and circumstance execute will not affect the functioning of an otherwise correct program.

Re:Falsehoods programmers believe about undefined behavior by asgeir on Monday November 28, 2022 @07:37AM (#33772842)
The compiler may make optimization decisions based on the presence of UB so the code only needs to be present.

The compiler can't know that an arbitrary part of a function is unreachable if, for instance, that code path is controlled by a parameter or global state since that equates to solving the halting problem.

https://blog.llvm.org/2011/05/what-every-c-programmer-should...

Re:Falsehoods programmers believe about undefined behavior by haberman on Monday November 28, 2022 @09:27AM (#33773971)
I don't think this is correct. If UB is never invoked, the program is conforming and must be translated/executed correctly. DR #109 specifically states that a compiler may not fail to translate a strictly conforming program just because some possible executions could trigger UB: https://www.open-std.org/jtc1/sc22/wg14/docs/rr/dr_109.html
Re:Falsehoods programmers believe about undefined behavior by asgeir on Monday November 28, 2022 @09:55AM (#33774345)
The undefined behavior can still affect code generation even though the program gets translated.

It's just that the generated code may not do exactly what you expect it to do because the presence of undefined behavior allowed the compiler to make assumptions which may surprise you.

Re:Falsehoods programmers believe about undefined behavior by haberman on Monday November 28, 2022 @10:05AM (#33774481)
DR 109 allows that a program may be strictly conforming even if some possible executions of the program invoke UB:

> A conforming implementation must not fail to translate a strictly conforming program simply because some possible execution of that program would result in undefined behavior.

This text specifically allows for the case that a program is strictly conforming even if there is a possible execution that invokes UB. If a program is strictly conforming, it must produce the correct behavior.

I would challenge you to show an example where GCC or Clang will break the correctness of a program on account of UB that is not reached during program execution.

Here is an example where GCC and Clang specifically respects the correctness of a program as long as it does not reach the UB: https://godbolt.org/z/befWah77W

Re:Falsehoods programmers believe about undefined behavior by asgeir on Monday November 28, 2022 @11:03AM (#33775273)
It seems that you may be correct.

At the very least I am unable to construct a counter-example so I concede this point

Re:Falsehoods programmers believe about undefined behavior by leni536 on Monday November 28, 2022 @07:22AM (#33772726)
No, it isn't. The line with UB has to be reached in the execution for the execution to be undefined.
Re:Falsehoods programmers believe about undefined behavior by throwaway294566 on Monday November 28, 2022 @07:45AM (#33772931)
Yes, but there is more to it: The compiler may assume that UB will never happen intentionally and optimize out the branch containing UB.
Re:Falsehoods programmers believe about undefined behavior by tsimionescu on Monday November 28, 2022 @08:13AM (#33773210)
The compiler isn't required to know if code is unreachable. For example, `if (fermats_last_theorem_is_wrong()) {*p == 1} ` will imply to a modern compiler that p is not NULL in the scope where it is defined, even if we now know that this code will never be reached (assuming that function is correctly written).

Or, here is a better example:

  for(int i=0; i > n;) {
  }
  *p=1;
Here the compiler will assume that the loop terminates, since non-terminating loops without side-effects are UB in C++.

Then, since the loop terminates, p will be dereferenced, so it can be assumed that p is never NULL, so any NULL checks for p in this context can be ellided.

This remains valid even if you call this function with an n such that *p=1 is unreachable.

Re:Falsehoods programmers believe about undefined behavior by caf on Monday November 28, 2022 @09:03AM (#33773743)
If a some translation unit defines int fermats_last_theorem_is_wrong(void) { return 0; } then a well-defined program can execute that sequence even with p equal to NULL.
Re:Falsehoods programmers believe about undefined behavior by haberman on Monday November 28, 2022 @09:15AM (#33773855)
The compiler is required to successfully translate a program that contains unreachable UB, and such a program is conforming if undefined behavior is never reached. This is explicitly spelled out in https://www.open-std.org/jtc1/sc22/wg14/docs/rr/dr_109.html

> Furthermore, if every possible execution of a given program would result in undefined behavior, the given program is not strictly conforming. A conforming implementation must not fail to translate a strictly conforming program simply because some possible execution of that program would result in undefined behavior. Because foo might never be called, the example given must be successfully translated by a conforming implementation.

Re:Falsehoods programmers believe about undefined behavior by account42 on Monday November 28, 2022 @10:11AM (#33774557)
Sad to see this downvoted here.

To hammer it in, the consider the following:

if(p) *p = 0;

*p is undefined if p is NULL. But as long as that statement is never reached when p is NULL that is not a problem. That is trivially true in this case, but the same applies even for more complex relations between the conditional and the undefinedness of the statement.

Or put another way, in

  int * p = 0;
  if(fermats_last_theorem_is_wrong()) {
    p = malloc(sizeof(int));
    *p = 0;
  }
  if(p) {
    puts("fermats last theorem is wrong\n");
  }
  if(fermats_last_theorem_is_wrong()) {
    *p == 1;
  }
  if(p) {
    puts("fermats last theorem is wrong\n");
  }
the compiler is not allowed to remove the if(p) checks and unconditionally print that fermats last theorem is wrong unless it can prove that fermats_last_theorem_is_wrong() is true.

In fact, something having undefined behavior and the compiler being allowed to assume that it is unreachable are effectively the same thing. Which is why the spec for std::unreachable() and the compiler-specific intrinsics that preceded it is that it invokes undefined behavior and not more complex wording.

Re:Falsehoods programmers believe about undefined behavior by tsimionescu on Tuesday November 29, 2022 @03:33AM (#33785053)
Yes, my example with Fermat's last theorem was completely wrong, not sure why it seemed plausible to me when I wrote it, it's obviously just a guard, and it obviously is not UB to have a pointer write behind an If...
Re:Falsehoods programmers believe about undefined behavior by mnw21cam on Monday November 28, 2022 @09:51AM (#33774299)
Presumably (and really don't take my word for this - I'm a Java bod) the p dereference needs to come before the p NULL check for the p NULL check to be ignored. (Otherwise, how can a NULL check protect a dereference.) And therefore, the NULL check would be unreachable as well, and therefore never executed and not relevant.

Please correct my misunderstanding...

Re:Falsehoods programmers believe about undefined behavior by adgjlsfhk1 on Monday November 28, 2022 @10:03AM (#33774464)
Nope, the C spec is just really bad. The NULL check is allowed to be removed.
Re:Falsehoods programmers believe about undefined behavior by mnw21cam on Monday November 28, 2022 @11:03AM (#33775271)
So the pseudocode:

  if (p is not NULL) {
    dereference p
  }
is UB? That's crazy, but not actually unbelievable.
Re:Falsehoods programmers believe about undefined behavior by dllthomas on Monday November 28, 2022 @11:21AM (#33775518)
I am not certain of the language lawyer side of it, but no reasonable (or even moderately unreasonable) compiler would omit that check because of the guarded behavior; way too much code would break, probably including that compiler's standard library.
Re:Falsehoods programmers believe about undefined behavior by UncleMeat on Monday November 28, 2022 @12:15PM (#33776302)
The poster is just completely wrong. A compiler is required to emit the branch in this case.
Re:Falsehoods programmers believe about undefined behavior by tsimionescu on Tuesday November 29, 2022 @03:39AM (#33785094)
There's no misunderstanding on your part, just some muddled thinking on my part for the first example.
Re:Falsehoods programmers believe about undefined behavior by account42 on Monday November 28, 2022 @10:02AM (#33774445)
> For example, `if (fermats_last_theorem_is_wrong()) {*p == 1} ` will imply to a modern compiler that p is not NULL in the scope where it is defined, even if we now know that this code will never be reached (assuming that function is correctly written).

No it won't. Otherwise all precondition checks would be meaningless. The compiler can only assume that it isn't null when fermats_last_theorem_is_wrong() is true. If the compiler cannot reason about fermats_last_theorem_is_wrong() then it can't just ignore it.

> for(int i=0; i > n;) { } *p=1;

> Here the compiler will assume that the loop terminates, since non-terminating loops without side-effects are UB in C++.

Infinite loops are UB precisely because otherwise the compiler would NOT be able to reason about this. So yes, here the comiler can assume that p is a valid pointer - but only because it can also assume that *p=1 is NOT dead. This falls entirely under the umbrella of behavior not being defined at all after you execute undefined behavior (the infinite loop).

> This remains valid even if you call this function with an n such that *p=1 is unreachable.

Only because the loop itself is undefined behavior in that case.

Re:Falsehoods programmers believe about undefined behavior by AstralStorm on Monday November 28, 2022 @10:26AM (#33774747)
That is not an infinite loop, it has an exit condition on i > n.

An infinite loop is not UB. It is well defined. Just the definition of a terminating loop is a bit funny.

> An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression, may be assumed by the implementation to terminate.

Plus:

> An omitted controlling expression is replaced by a nonzero constant, which is a constant expression.

The compiler may only use as-if rule here if it can deduce the value is constant or limited. It can (but does not have to) assume the loop will end due to the conditional and lack of the other features. Which is still defined behavior. (since C11 at least, probably earlier)

Re:Falsehoods programmers believe about undefined behavior by haberman on Monday November 28, 2022 @10:44AM (#33774973)
> An infinite loop is not UB. It is well defined.

From C11:

> An iteration statement whose controlling expression is not a constant expression,156) that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate.157)

> 157)This is intended to allow compiler transformations such as removal of empty loops even when termination cannot be proven.

This makes infinite loops UB unless they fall under one of these exceptions (constant controlling expression, performs I/O, accesses volatile objects or atomics).

Re:Falsehoods programmers believe about undefined behavior by AstralStorm on Monday November 28, 2022 @12:43PM (#33776726)
Not particularly. It does not allow the compiler to assume true infinite loops can terminate.

It only allows the compiler to assume loops with non-constant expressions can terminate. (And a bunch of other exceptions where it cannot.) A true infinite loop has a constant expression as the conditional and cannot be optimized away based on this rule.

What this rule allows the compiler to do is to optimize away loops where number of iterations are known ahead of time if it has a non-constant expression condition.

Re:Falsehoods programmers believe about undefined behavior by haberman on Monday November 28, 2022 @02:08PM (#33778062)
> It only allows the compiler to assume loops with non-constant expressions can terminate. (And a bunch of other exceptions where it cannot.)

That is basically what I said: loops can be assumed to terminate, except for an enumerated set of exceptions where infinite loops are allowed.

You seem to be defining "true infinite loop" to mean "while(1)" or "for(;;)" exclusively. But "for (int i = 0; i < 1;)" is also an infinite loop, and that one is UB since the controlling expression is not constant.

> What this rule allows the compiler to do is to optimize away loops where number of iterations are known ahead of time if it has a non-constant expression condition.

If the number of iterations are known ahead of time, then it is not an infinite loop so I don't see how the rule would apply.

The rationale for making infinite loops UB is described here: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1528.htm

Specifically, it allows for transformations like turning:

    for (p = q; p != 0; p = p -> next) {
        ++count;
    }
    for (p = q; p != 0; p = p -> next) {
        ++count2;
    }
into:

    for (p = q; p != 0; p = p -> next) {
            ++count;
            ++count2;
    }
Re:Falsehoods programmers believe about undefined behavior by AstralStorm on Monday November 28, 2022 @05:06PM (#33780545)
Infinite loop with a constant expression is defined behavior.

for (;;) { }; cannot be assumed to have finite number of executions. Therefore, it cannot be optimized away to finite execution.

You're showing potentially infinite loops with non-constant expression as condition.

for (; i != 0; ) {} can be assumed to have finite number of executions and can be optimized unless i is constant expression.

Re:Falsehoods programmers believe about undefined behavior by haberman on Tuesday November 29, 2022 @11:53AM (#33789977)
> Infinite loop with a constant expression is defined behavior.

Yes, because a constant controlling expression is one of the enumerated exceptions to the rule against infinite loops.

> You're showing potentially infinite loops with non-constant expression as condition.

"for (int i = 0; i < 1;)" is an infinite loop, full stop. It is not potentially infinite, it is infinite under all possible executions. It is UB because it does not fall under one of the enumerated exceptions where an infinite loop is allowed.

I think we are saying the same thing, the only difference is that you seem to be defining "infinite loop" to only include loops with constant controlling expressions, whereas I am defining it to mean "any loop that does not terminate."

Re:Falsehoods programmers believe about undefined behavior by tsimionescu on Tuesday November 29, 2022 @03:38AM (#33785091)
Yes, the part about Fermat's last theorem I wrote is actually obviously wrong, I don't know what exactly I was thinking when I wrote it.

The example with the loop though is more to point out that our native understanding of "unreachable" may be different than the compiler's. The same problem could occur without invoking UB if we un-conditionally generate a SIGKILL to our own process and then dereference a NULL pointer - the compiler doesn't know what SIGKILL does, so it doesn't know the UB is unreachable in practice, so it may reorder etc.

Re:Falsehoods programmers believe about undefined behavior by gpderetta on Tuesday November 29, 2022 @05:59AM (#33786030)
> the compiler doesn't know what SIGKILL does, so it doesn't know the UB is unreachable in practice, so it may reorder etc

It is the reverse. If the compiler doesn't know what raise(SIGKILL) (or really, any other function it cannot see through) does, it has to assume that it can potentially never terminate, abort or otherwise change the control flow, so it can't safely reorder across it. And even if it does return, it could still inspect memory and observer the violation of the as-if rule.

That's why for example pthread_mutex_lock/unlock worked correctly for the most part even before C++ got a proper concurrent memory model.

Re:Falsehoods programmers believe about undefined behavior by indymike on Monday November 28, 2022 @09:39AM (#33774100)
Unreachable code that contains UB can have side effects at compile time, that potentially could cause problems with other code when executed (mostly bad compiler optimizations but with c... surprises happen). This is what makes C so fun.
Re:Falsehoods programmers believe about undefined behavior by zajio1am on Monday November 28, 2022 @07:31AM (#33772790)
That (or more general 13-16) also does not make sense to me. Most UB (although perhaps not all, so it may be true for some obscure UB, or ones with constant arguments) are undefined for specific dynamic conditions, so it is not 'line with UB' but more 'line with UB under specific program state', e.g. expression 'a / b;' has UB if b == 0, 'a->item' has UB if b == NULL, or 'a * 4' has UB if a is int and large enough to cause overflow. If the expression is not executed, the dynamic conditions are not there, and therefore there is no UB.
Re:Falsehoods programmers believe about undefined behavior by mattarm on Monday November 28, 2022 @11:59AM (#33776047)
FYI, an ODR violation (https://en.wikipedia.org/wiki/One_Definition_Rule) is an example of non-dynamic UB.
Re:Falsehoods programmers believe about undefined behavior by Semaphor on Monday November 28, 2022 @07:48AM (#33772953)
This [0] is the linked article for that point, I don’t do systems programming, so I can’t say I understand if this is trivial or not ;)

[0]: https://www.ralfj.de/blog/2020/07/15/unused-data.html

Re:Falsehoods programmers believe about undefined behavior by account42 on Monday November 28, 2022 @09:54AM (#33774334)
The linked article argues that making storing invalid values in the bool undefined (rather than just using such bools) is important because otherwise the compiler is not allowed to move the code (without adding appropriate checks that it would have been reached). The compiler can only make that transformation if it has no effect on any cases where the code wasn't reached at all, which includes not introducing any new UB in those cases. Predrag conclusion that this means having UB in dead code matters is simply wrong.
Re:Falsehoods programmers believe about undefined behavior by nickitolas on Monday November 28, 2022 @04:14PM (#33779774)
The blog post does not support the assertion that unreachable UB which is not executed is a problem. It starts by saying

> The Rust compiler has a few assumptions that it makes about the behavior of all code. Violations of those assumptions are referred to as Undefined Behavior.

Then it says

> In other words, even just constructing, for example, an invalid bool, is Undefined Behavior—no matter whether that bool is ever actually “used” by the program.

The blog post is talking about how just creating an invalid/trap/niche representation for a value is UB. The act of creating the value is the UB here, not the usage. So, in this case the UB is most definetely reachable and executing (The act of creating the invalid value).

Re:Falsehoods programmers believe about undefined behavior by tempodox on Monday November 28, 2022 @10:05AM (#33774485)
Code that represents UB doesn't have to be executed for the compiler to react to it.
Re:Falsehoods programmers believe about undefined behavior by AstralStorm on Monday November 28, 2022 @10:20AM (#33774674)
More specifically, the linker, not the compiler.

The compiler has to respect DR 106. The linker, however, is allowed to write garbage into code.

Re:Falsehoods programmers believe about undefined behavior by gpderetta on Monday November 28, 2022 @10:27AM (#33774759)
the standard does not distinguish the linker from the compiler.