News for hackers. Stuff that matters.
Re:Falsehoods programmers believe about undefined behavior by olliej on Monday November 28, 2022 @07:10AM (#33772615)
The biggest problem with UB, as interpreted by C/C++ compilers is the unfounded belief that the spec says that you can assume any path that leads to UB is defacto incorrect and can be ignored. That's the bogus logic required for steps 13-16 to be true.

That interpretation of the specification language is clearly bogus as that means more or less all C and C++ programs can be compiled to a single return instruction - when compiler devs introduced this new new interpretation of UB, it turned out they had to then go through and add a bunch of exceptions for basics like memset, memcpy, and memmove could work without falling afoul of their invented nonsense.

Re:Falsehoods programmers believe about undefined behavior by maccard on Monday November 28, 2022 @07:45AM (#33772928)
This is an absurd reduction of the current situation.

> That interpretation of the specification language is clearly bogus as that means more or less all C and C++ programs can be compiled to a single return instruction - when compiler devs introduced this new new interpretation of UB, it turned out they had to then go through and add a bunch of exceptions for basics like memset, memcpy, and memmove could work without falling afoul of their invented nonsense.

I've never heard of anything like this, have you any sources for this happening?

Re:Falsehoods programmers believe about undefined behavior by rightbyte on Monday November 28, 2022 @08:01AM (#33773077)
Read the gcc mailing list. There is alot of user hostile "fix your broken program" from language lawyers.

I think aliasing rules messes up memcpy if you are dogmatic?

Re:Falsehoods programmers believe about undefined behavior by maccard on Monday November 28, 2022 @08:21AM (#33773286)
Theres a big difference between "the GCC mailing list is full of language lawyers" and "memcpy doesn't work because UB and the compiler Devs fixed it by adding an exception for it".

> I think aliasing rules messes up memcpy if you are dogmatic?

I'm _not_ dogmatic or a language lawyer and aliasing is a total pain in the ass, but that's a whole different topic to compilers getting fundamentals wrong because of language lawyering.

Re:Falsehoods programmers believe about undefined behavior by ilyt on Monday November 28, 2022 @08:20AM (#33773274)
To be fair it is extremely easy to write broken C/C++ program
Re:Falsehoods programmers believe about undefined behavior by anonymoushn on Monday November 28, 2022 @08:54AM (#33773656)
I think memcpy is fine because it operates on a special type that is allowed to alias anything. Strict aliasing has other fun issues like the impossibility of implementing malloc-like functions or the lack of any requirement for access to non-char members of structs to work at all[0].

[0]: https://stackoverflow.com/questions/49298704/how-reason-abou...

Re:Falsehoods programmers believe about undefined behavior by olliej on Monday November 28, 2022 @11:25AM (#33775589)
memcpy is UB because it reads padding bytes in structs.

memmove fails as it requires pointer comparisons between separate allocations, which is UB.

Re:Falsehoods programmers believe about undefined behavior by anonymoushn on Monday November 28, 2022 @12:16PM (#33776306)
Sounds like we shouldn't use memcpy on structs containing padding!
Re:Falsehoods programmers believe about undefined behavior by maccard on Tuesday November 29, 2022 @10:59AM (#33789176)
No, memcpy is fine because it's a language level function. What you _can't_ do is implement your own memcpy alternative safely.
Re:Falsehoods programmers believe about undefined behavior by nayuki on Monday November 28, 2022 @01:18PM (#33777285)
memcpy() and memmove() are language-level functions. They are allowed to do things that cannot be implemented in user code. The C standard library's implementation of these functions have tight integration with the compiler to ensure that it can implement all the required semantics without triggering undefined behavior.
Re:Falsehoods programmers believe about undefined behavior by leni536 on Monday November 28, 2022 @05:26PM (#33780748)
reading padding bytes is not UB.

Comparing unrelated pointers is unspecified, not undefined. It's also an implementation detail of memmove. It can use a magical total order comparison of pointers that is otherwise unavailable to users of the language.

Re:Falsehoods programmers believe about undefined behavior by olliej on Monday November 28, 2022 @11:38AM (#33775780)
memset, memcpy, and memmove, etc all depend on UB.

memset and memcpy read and write padding bytes, which is UB. If you try to say memset/cpy is a "primitive operation", then you're still stuck with the "correct" struct initialization (memset, bzero) being UB if you did something like:

    void f(bool condition) {
      if (condition) {
        struct structwithpadding thing;
        bzero(&thing, sizeof(thing));
      } else {
        printf("wat\n");
      }
    }
The ludicrously broad definition of what UB apparent allows, this can be "optimized" to

    void f(bool condition) {
      printf("wat\n");
    }
Because bzero() writes to padding, and that is UB, which means the condition==true branch "can't happen" \o/

memmove falls to UB much requires pointer comparisons between pointers to different objects, which is UB.

Re:Falsehoods programmers believe about undefined behavior by ilyt on Monday November 28, 2022 @08:19AM (#33773265)
Wouldn't really be a problem if the design of those languages wasn't horribly ridden with footguns.

Compiler designers just chose the notion of "obviously user wouldn't invoke the footgun, so any code that would be causes by footgun can be removed"

Re:Falsehoods programmers believe about undefined behavior by MushyQuadrant on Monday November 28, 2022 @12:15PM (#33776301)
This is a necessary consequence (in the formal logic sense) of the "principle of explosion"[1] for optimizing compilers. If the compiler assumes some undefined behavior does not occur, but in fact that undefined behavior does occur, all of conclusions it makes which depend, directly or indirectly, on the contradicted assumption are invalid.

Since, in the process of optimization, optimizing compilers make conclusions about parts of a program based on other parts of the program which may be executed later, undefined behavior in one part of a program causes invalid conclusions about other parts which may be executed earlier. Therefore undefined behavior cannot be contained to just parts of a program executed after a particular part, if the compiler tries to optimize parts of the program based on parts that may be executed later.

[1]: https://en.wikipedia.org/wiki/Principle_of_explosion