News for hackers. Stuff that matters.
Re:Undefined behavior in C is a reading error by layer8 on Thursday May 20, 2021 @08:06PM (#27229026)
It goes further than that. The debates around UB a more about whether the compiler can assume that there are no buffer overflows and perform optimizations based on that. For example, if you have a local variable `char buffer[16]`, and there is an access `buffer[i]`, should the compiler be allowed to derive that 0 <= i < 16, and perform optimizations on other uses of `i` based on that "knowledge"? If all bets are off for `buffer[i]` when i < 0 or i >= 16, why shouldn't it? But some argue that the compiler shouldn't, exactly because such automated formal reasoning can lead to arbitrarily wide reaching effects. The problem is that it's hard to see where a middle ground could be.
Re:Undefined behavior in C is a reading error by sgtnoodle on Friday May 21, 2021 @04:08AM (#27231598)
If the array index is unconditional, then sure that seems like a reasonable undefined behavior? If the access is conditional and the compiler can't infer whether the branch is taken, though, then it shouldn't make that assumption.

This reminds me of a bug in the msp430 variant of gcc. The compiler would replace "x <= literal" with "x < literal + 1" because it generated more efficient code. If the literal was UINT_MAX, though, the compiler would trudge ahead and roll over the literal to 0. It would then subsequently reason that the comparison was unconditionally false, and then completely optimize away the nominal path of my code. That was a frustrating day of debugging!

Re:Undefined behavior in C is a reading error by rectang on Thursday May 20, 2021 @08:49PM (#27229300)
The result is a perpetually unstable situation fostering inevitable errors when the programmer is surprised by the compiler. There's no remedy except to move away from C to more tightly specified languages.
Re:Undefined behavior in C is a reading error by pjmlp on Friday May 21, 2021 @03:52AM (#27231523)
The problem is ecosystems like UNIX based platforms, that will never move to something else.

The only solution is to throw them away, e.g. Android style.

Re:Undefined behavior in C is a reading error by throwaway2048 on Friday May 21, 2021 @04:24AM (#27231670)
Android threw away a unix based platform? News to me.
Re:Undefined behavior in C is a reading error by pjmlp on Friday May 21, 2021 @04:41AM (#27231772)
It did, ask termux guys.

The Linux kernel is an implementation detail.

There is nothing UNIX related exposed to userspace.

Java and Kotlin applications, while NDK APIs are clearly defined,

https://developer.android.com/ndk/guides/stable_apis

Or Treble Project for that matter,

https://source.android.com/devices/architecture/hidl

Try to use private APIs (which is what the Linux underpinnings are) and the application might be blessed with being killed by the OS.

Re:Undefined behavior in C is a reading error by im3w1l on Friday May 21, 2021 @05:02AM (#27231866)
What I never see mentioned in this type of discussion is what this compiler trick buys us. Like how much faster does my program run because of deductions like these? And are there any patterns that particularly benefit from it? Could they hint the constraint manually?
Re:Undefined behavior in C is a reading error by mpcjanssen on Friday May 21, 2021 @06:07AM (#27232213)
There are plenty of examples in this thread alone.

In my opinion hinting won't help. Everyone who cares about the performance gain will enable the hint (which I expect will be almost everyone) so you have won nothing but added noise with the hint.

Re:Undefined behavior in C is a reading error by vyodaiken on Friday May 21, 2021 @08:53AM (#27233387)
There is not a single compelling example. Just contrived examples with dubious speedups and obvious errors.