News for hackers. Stuff that matters.
Re:Falsehoods programmers believe about undefined behavior by tonetheman on Monday November 28, 2022 @07:26AM (#33772748)
I 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. :)

Re:Falsehoods programmers believe about undefined behavior by sokoloff on Monday November 28, 2022 @07:37AM (#33772839)
You’ve never had an integer overflow? You’ve never dereferenced a null pointer?

I suspect every C programmer causes undefined behavior in their first 50 hours of writing C.

Re:Falsehoods programmers believe about undefined behavior by marcosdumay on Monday November 28, 2022 @08:37AM (#33773467)
It's quite safe to say that every hand-off between code written by different developers can exercise UB in a few different ways.
Re:Falsehoods programmers believe about undefined behavior by jxf on Monday November 28, 2022 @07:34AM (#33772815)
> I cannot say I have ever written anything that ran into or caused undefined behavior.

But how would you know? :)

Re:Falsehoods programmers believe about undefined behavior by unnah on Monday November 28, 2022 @08:55AM (#33773659)
These days it is not quite unknowable, if you always runs your programs with the address sanitizer and UB sanitizer on. They aren't perfect but they would provide a pretty good indicator of whether there was any UB or not.
Re:Falsehoods programmers believe about undefined behavior by hxhxhrra on Monday November 28, 2022 @07:44AM (#33772914)
I wouldn't call some cases of UB like overflowing a signed int or running into a dangling reference "actively pushing the compiler". I'm having a hard time believing you didn't ever run into these or similar issues having "programmed for a long time".

For other cases (e.g. aliasing, alignment), I'd agree that one is rather safe as long as the dangerous tools (e.g. reinterpret_cast) are not used.

Re:Falsehoods programmers believe about undefined behavior by jstimpfle on Monday November 28, 2022 @07:45AM (#33772923)
It's the same for me. Well, I've hit segfaults when dereferencing invalid pointers (including NULL). I've had some surprises with shifting and signed integers as well. But I'm not sure that I've hit strange behaviours late in development. All that stuff is just, oh, I got that wrong, let's fix it. At most - oh I hadn't known that detail about signed arithmetic.

If the compiler removes an important check or branch like in the post, and it isn't noticed in development chances are the code is dead, or not that important. And I'm not saying this to downplay the risks. A big risk is that a particular instance of UB is exploited only by a newer version of the compiler, after the code is already finished. But in general, the language remains a remarkably productive one that lets me get very close to where I want to be, very quickly.

Is it possible to know for sure that it's all correct and will continue to be correct with newer compilers? Not 100%, but maybe it's still a good tradeoff in some environments.

Re:Falsehoods programmers believe about undefined behavior by maccard on Monday November 28, 2022 @07:51AM (#33772988)
> 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 possible, and to do what you likely intended. That's not to say _all_ UB is as reasonable, but a significant amount of what you write relies on some implicit assumptions about UB for a compiler to output reasonable code.

Re:Falsehoods programmers believe about undefined behavior by archi42 on Monday November 28, 2022 @08:16AM (#33773231)
I think they meant "never written UB"? Which would still be a bold claim, much like "I never write bugs" (only features :P).
Re:Falsehoods programmers believe about undefined behavior by jstimpfle on Monday November 28, 2022 @08:09AM (#33773159)
Compilers use reasoning based on UB to optimize and simplify code. But that's not a complete argument why it's highly likely that a given code exhibits UB at run time.
Re:Falsehoods programmers believe about undefined behavior by tonyarkles on Monday November 28, 2022 @11:02AM (#33775265)
In a similar vein to "dereferenced a pointer", "null check on a pointer" is one of those things that often gets optimized out without the developer realizing it.
Re:Falsehoods programmers believe about undefined behavior by wyldfire on Monday November 28, 2022 @08:44AM (#33773544)
> I cannot say I have ever written anything that ran into or caused undefined behavior. And I have programmed for a long time.

If during that long time you were writing C or C++, it's very likely that you did but you may not have attributed the bugs you were fixing to Undefined Behavior. You may have considered them either to be your bugs or "compiler bugs that you worked around."

Re:Falsehoods programmers believe about undefined behavior by tonetheman on Monday November 28, 2022 @10:10AM (#33774552)
I have written in C/C++ and gotten my fair share of null pointers or broken things but all related to logic or my own programming errors. But to me those are just that logic errors or my own crappy code errors.

Back when I worked on VAX/Alpha (I am old) I absolutely had to get down and read assembly/binary to figure out the compiler was doing what I thought. And in some cases had to work around compiler bugs.

But for most of the more recent things I have written I just have not run into these types of errors. I am really boring or I write code that is boring ha.

It is fascinating that people study this stuff and interesting to read about, I just do not run into it.

Re:Falsehoods programmers believe about undefined behavior by MushyQuadrant on Monday November 28, 2022 @11:55AM (#33776002)
> I cannot say I have ever written anything that ran into or caused undefined behavior. And I have programmed for a long time.

Perhaps this is true, or perhaps you are just unaware of the undefined behavior you have relied on.

What I can say for sure, is that all of the non-trivial C++ programs written by a large team which I have worked on in my career have invoked some undefined behavior. Usually, the compiler does do "the right thing" despite the error, but sometimes it blows up in our face, and causes incredibly hard to debug bugs, such as a pointer appearing to simultaneously point to two different addresses, because an array was declared as two different sizes in two different translation units.

Even when I spot the instances of undefined behavior, it's often hard to convince other developers that, although this seems to behave correctly now, it could break in unexpected, hard to debug ways at any time in the future.