I have a suggestion - it may be completely off the wall, but hear me out. C needs a mechanism to declare in a source file that certain behaviour must be defined, where that behaviour is undefined in the C standard but the target architecture behaves the defined way. Then, when the source is compiled on the target architecture it works as expected, but when compiled on something else it refuses to compile. For example…
Suggestions like this are unfortunately quite infeasible (they fall under the hat of "define all the behaviors"), and the talk I linked in the post goes into why: https://www.youtube.com/watch?v=yG1OZ69H_-o (I'm the post's author.)
Falsehoods programmers believe about undefined behavior
141–150 of 233 posts
Re: Falsehoods programmers believe about undefined behavior
#142Earlier quoted context omitted.
Suggestions like this are unfortunately quite infeasible (they fall under the hat of "define all the behaviors"), and the talk I linked in the post goes into why: https://www.youtube.com/watch?v=yG1OZ69H_-o (I'm the post's author.)
He's not trying to define all behaviours, he wants a static assertion that the compiler will compile certain behaviours in a specific way. Seems very different.
`-fwrapv` is an attempt toward doing such a thing for signed integer overflow specifically. I bet it's a pain to maintain because it significantly increases the surface area of the compiler that needs to be tested.
I don't believe doing this for more cases is feasible at the level of the compiler. Things like CHERI where the hardware and OS help are much more viable, in my book: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
Re: Falsehoods programmers believe about undefined behavior
#143Divide by zero was one that I always believed would give segmentation fault…but that is totally not the case; I’ve gotten Infinity as a return once… I forget what compiler gives what…
Re: Falsehoods programmers believe about undefined behavior
#144> Here's the list of guarantees compilers make about the outcomes of undefined behavior: > That's the whole list. No, I didn't forget any items. Yes, seriously. I’m a little disappointed there isn’t an empty or in the middle there. I imagine screen readers would announce something like “list, zero items”.
I wish there were a best practices doc + a matching linter for accessibility things like this.
Re: Falsehoods programmers believe about undefined behavior
#145I have a suggestion - it may be completely off the wall, but hear me out. C needs a mechanism to declare in a source file that certain behaviour must be defined, where that behaviour is undefined in the C standard but the target architecture behaves the defined way. Then, when the source is compiled on the target architecture it works as expected, but when compiled on something else it refuses to compile. For example…
The purpose of C is to be easy to implement on new systems. You're never going to get a lot of traction adding anything to C proper.
Re: Falsehoods programmers believe about undefined behavior
#146I am always amazed by articles like these. This guy thoroughly covers this topic. I cannot say I have ever written anything that ran into or caused undefined behavior. And I have programmed for a long time. Is this just an interesting topic or do people actively push the compiler to see what it will do? Maybe I am a boring/simple programmer. :)
> I cannot say I have ever written anything that ran into or caused undefined behavior. And I have programmed for a long time. Assuming you're working with C or C++, you've never: - Dereferenced a pointer? - written a loop with a signed index? - Accessed an index in an array or a vector? - Used a variable? - Used an optimising compiler? Compilers use undefined behaviour in all of the above scenarios as much as possib…
Re: Falsehoods programmers believe about undefined behavior
#147Earlier quoted context omitted.
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...
Nope, the C spec is just really bad. The NULL check is allowed to be removed.
if (p is not NULL) {
dereference p
}
is UB? That's crazy, but not actually unbelievable.Re: Falsehoods programmers believe about undefined behavior
#148Earlier quoted context omitted.
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. edit: It seems that I am incorrect
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 inv…
At the very least I am unable to construct a counter-example so I concede this point
Re: Falsehoods programmers believe about undefined behavior
#149Re: Falsehoods programmers believe about undefined behavior
#150It does seem odd to just "lump unspecified behavior and implementation-defined behavior together" in a side-note under the heading of "implementation-defined behaviour", when that term has a precise technical meaning in the C and C++ specifications.