Earlier quoted context omitted.
> For example, if signed integer overflow yielded an unspecified result rather than causing undefined behavior, I wonder if any implementations would be adversely affected. I suspect so - makes it harder to reason about loop counts because the compiler can't necessarily guarantee that an incremented loop counter won't become negative and thus the loop needs to iterate more. E.g. something like for (int i=param; i Tha…
for (int i=param; i does not have a guaranteed loop count with the current rules. The loop body will execute 16 times if param <= INT_MAX-16, but if the expression "param + 16" can overflow, the behavior is undefined. (I'm assuming param is of type int.)
Undefined behavior in C is a reading error
101–110 of 503 posts
Re: Undefined behavior in C is a reading error
#102First: I do dislike how hard it is to avoid some UB / how impractical some of the rules are. But I also think a lot of discussions of this topic caricaturize compiler writers to a ridiculous degree. Almost describing them to write optimization passes looking for UB so they can over-optimize something, while cackling loudly in glee about all the programs they can break. The set of people doing so overlaps with the set…
The inflamed backlash should tell you just how damaging it is to impose silent failure on meticulously written, previously fine programs.
Re: Undefined behavior in C is a reading error
#103If C is just a portable assembler then what if the assembly itself has undefined behaviour. :)
This exists, but the effect of undefined behavior in CPU architectures is a little bit more forgiving than the interpretation of UB in C to mean "literally the entire program has no meaning". Instead, usually the program will execute correctly up to the invalid instruction, and then something happens, and then the CPU will continue executing from that state. It's actually fairly difficult to build an instruction with…
Re: Undefined behavior in C is a reading error
#104Re: Undefined behavior in C is a reading error
#105Earlier quoted context omitted.
Skimmed the article and didn't see a reference to it, you may be interested to know that our good friends and protectors at the NSA may have stumbled on to Meltdown-like issues in the mid 90s https://en.wikipedia.org/wiki/Meltdown_(security_vulnerabili...
I see the NSA strategy for 'securing' the nation against technology threats in their 'unique' way was going strong back in 1995.
Re: Undefined behavior in C is a reading error
#106Re: Undefined behavior in C is a reading error
#107For instance overflowing arithmetic is well specified on every architecture - the behaviour may be different on say sparc vs an hc12 vs x86, but on any of those architectures it will always be the same. Yet compiler devs have instead decided that it is undefined and so can be treated however they like.
There are so many of these UB that could be unspecified that it’s absurd - UB should be reserved for when there outcome is not consistent - UaF, invalid pointers, out of range accesses, etc
Re: Undefined behavior in C is a reading error
#108Earlier quoted context omitted.
>Somehow the author seems to think that this is not what the wording intended, but they also fail to explain what they think should happen here. What "should" happen is that the implementation-defined behavior in the assert matches the implementation-defined behavior in the calculation. That is, assert(a+100>a) should produce the same result as int x = a + 100; int y = a; if (x > y) ; else printf("assertion failed\n"…
That behaviour is no less undefined in the process of overflow.
Re: Undefined behavior in C is a reading error
#109Earlier quoted context omitted.
Anecdotally, I find about as many bugs in my Java code at work, as I do in my hobby C code, including "dereferencing null pointers" aka NullPointerExceptions.
NPE isn't really a relevant comparison point - it raises an exception of sorts in both languages. Accessing an array past its end, accessing freed memory, reading uninitialized memory seem more apt.
C doesn't trade performance away, so if you'd like to pay the price of these checks, it's on you, the programmer to add them in. C programmers have to put in extra effort to make the executables safer and output correct results.
Other languages trade off performance for safety and/or correctness. The programmers using them have to put in extra effort to make the executables run as fast as they do in C.
Ultimately, programmers tend to make rational choices, and the stockholm syndrome mentioned above is really just C programmers dealing with the consequences of the trade-off they made by using C.
Re: Undefined behavior in C is a reading error
#110Reading error or not, the ship has sailed long ago. As the article notes, C99 formalized the current UB interpretation. What C89 or K&R said is more just historical curiosity than of any real relevance today. I guess you could construct an argument that gcc should disable some optimizations when invoked with -std=c89, but I doubt anyone really cares at this point enough to justify the maintenance burden. C is a minef…
I think it's doable to make some of the UB issues considerably less painful. E.g. define facilities to check for overflow safely, add facilities for explicit wrapping operations.