News for hackers. Stuff that matters.
Re:Defining the Undefinedness of C (2015) [pdf] by simias on Wednesday July 26, 2017 @12:19PM (#14857576)
That's a good explanation for invalid memory accesses and divisions by zero but I'm not aware of many architectures where addition overflow traps (it can trap on MIPS but there's an other instruction that simply wraps around). That being said I don't have an encyclopedic knowledge of instruction sets.

I could be wrong though, after all compilers back then were a lot less clever than now so maybe they couldn't really make use of this information. I'd be curious to get a first hand testimony as to why certain UB exist in the first place.

Re:Defining the Undefinedness of C (2015) [pdf] by pjmlp on Wednesday July 26, 2017 @01:45PM (#14858370)
As someone that got introduced to C via SmallC and Turbo C 2.0 for MS-DOS, and avid reader of "The C User's Journal", one of the reasons was that ANSI C89 was supposed to standardize existing variants outside UNIX.

No compiler vendor back then wanted to give up on their own specific deviations, given their existing customer base, proprietary OSes and extensions.

So many of those vendor specific features got tucked away into implementation specific and UB buckets.

Re:Defining the Undefinedness of C (2015) [pdf] by tom_mellior on Wednesday July 26, 2017 @03:25PM (#14859340)
This is not first hand, but the commonly given reason for undefined signed integer overflow is that it allows for mathematically correct reasoning: x < x + 1 is true for all x. Or rather, the compiler may assume that it is true and optimize accordingly, since the programmer is trusted to avoid the undefined overflowing case.
Re:Defining the Undefinedness of C (2015) [pdf] by iainmerrick on Wednesday July 26, 2017 @09:12PM (#14861857)
"Mathematically correct" doesn't seem like the right way to think about it, since the whole problem is that it isn't correct!
Re:Defining the Undefinedness of C (2015) [pdf] by tom_mellior on Thursday July 27, 2017 @02:44AM (#14863137)
Yes, but it wouldn't be correct the other way round either. Wraparound on signed overflow is a well-defined operation on CPUs, but it is nonsensical. In almost no computation will it give a result that is meaningful in the context of what the program is trying to do. If you try to compute the average of two positive numbers as (a + b) / 2 but get a negative result, it would be of no use if this were "well-defined".

So from the point of view of a C compiler that exploits undefined signed overflow, if your code could overflow, it was probably already buggy, and you should have fixed it.

(One problem with this line of reasoning is that it kind of also applies to unsigned numbers, but there C goes the other way.)

Re:Defining the Undefinedness of C (2015) [pdf] by spc476 on Wednesday July 26, 2017 @04:27PM (#14859975)
The VAX. There's a bit in the condition codes register that indicates if overflow traps or not.

Then there's the matter if ((char )0) causes a trap or not; on systems without a MMU, it probably won't trap [1]. It's differences like overflow and memory references that cause most of the UB you see in C.

[1] On Linux, you can map a page to address 0. But that you can, doesn't mean you should.