News for hackers. Stuff that matters.
Re:Undefined Behavior in 2017 by jcelerier on Wednesday July 05, 2017 @07:51AM (#14701629)
The compiler would reject the following function :

int res(int a, int b) { return a + b; }

since it can be UB, for instance if a + b > INT_MAX.

Re:Undefined Behavior in 2017 by wamaral on Wednesday July 05, 2017 @08:09AM (#14701722)
Then by all means reject it, way better than blowing up in production.
Re:Undefined Behavior in 2017 by jerf on Wednesday July 05, 2017 @08:24AM (#14701798)
You just destroyed all C code in the world. At that level of paranoia, you've basically defined a brand new language, and if you're going to do that, why make it C at all?

Smart people with a ton of experience in C have already examined the possibility of defining a C dialect with much less undefined behavior in it. In fact, I believe it's been seriously tried more than once by independent groups. And the result has been the same each time; initial excitement and optimism gives way to total failure. It's simply way too deeply ingrained in the definition of C, backed by all the code in the world written in C. If you want less undefined behavior, your only choice is to leave C.

Re:Undefined Behavior in 2017 by wamaral on Wednesday July 05, 2017 @09:09AM (#14702075)
OP was talking about a compiler option, that most likely would not break anything unless you enable the flag.

As for choice, I'll take "here's what happens" vs "your code is in the hands of fate now" any day. Glad we do have choice.

Re:Undefined Behavior in 2017 by sullyj3 on Wednesday July 05, 2017 @08:42AM (#14701911)
I can't tell if this is satirical, but to be clear, you're proposing to disallow addition?
Re:Undefined Behavior in 2017 by sp332 on Wednesday July 05, 2017 @10:47AM (#14702780)
You could test whether overflow is going to happen first. Or use a math library that explicitly overflows with predictable results.
Re:Undefined Behavior in 2017 by dullgiulio on Wednesday July 05, 2017 @01:50PM (#14704627)
I know very few languages that do that. Even Rust doesn't when compiled for release. All for performance reasons.
Re:Undefined Behavior in 2017 by steveklabnik on Wednesday July 05, 2017 @02:22PM (#14704911)
Some additional points:

* It's not UB in Rust, it's guaranteed to be two's compliment overflow.

* It's still a "program error" and debug builds are required to panic.

* Non-debug builds are allowed to either panic or do the overflow, for performance reasons, as you mention. If it was feasible to always panic we'd do that; the current wording allows us to change it to do so in the future if it becomes feasible.

* If you want to do a checked add, you can, it's just not the default.

Re:Undefined Behavior in 2017 by sp332 on Wednesday July 05, 2017 @03:01PM (#14705215)
I might have lost the point in this long thread, but I'm pretty sure the suggestion was just to add it as an optional flag.
Re:Undefined Behavior in 2017 by wamaral on Wednesday July 05, 2017 @09:01AM (#14702029)
Rather define the behaviour of what happens when addition of two ints exceed the maximum allowed value.
Re:Undefined Behavior in 2017 by rspeer on Wednesday July 05, 2017 @12:17PM (#14703683)
You're changing your plan here. Maybe you could at least concede that the issue of UB is difficult and is not going to be solved by saying "why don't they just" on HN?