3 ms·
OK, what lesson exactly am I supposed to take away from this? Because the last sentence in that post is exactly my point: "... undefined behavior in C is a pr
by lisper 9y ago
OK, what lesson exactly am I supposed to take away from this? Because the last sentence in that post is exactly my point:
"... undefined behavior in C is a pretty scary thing if performance is not your only goal."
- saagarjha 9y agoYou mentioned this: > C's "undefined behavior" rules are insanity of the highest order, bordering on professional malpractice. They are the result of the pursuit of speed at the expense of all else. C's goal is performance (though not necessarily at the expense of all else). It's the compiler's job to make code that is as fast as possible. The article I linked was an attempt to show that it is very difficult to both have this goal as well as one in which you don't have "insane" undefined behavior. It's not that the C compiler authors are out to get you: "ha ha, lipser used undefined behavior! Let's break his code in the weirdest way"; rather, this seemingly poor behavior is borne out of a desire to have reasonable rules. Allowing the compiler to assume that a run runs ten times or that a pointer doesn't randomly change between statements is important.
- loup-vaillant 9y ago> It's the compiler's job to make code that is as fast as possible. This doesn't used to be the case, or we would have had something more like FORTRAN, with less pointers that alias all over the place. C was build so the programmer could optimise the program. The design of C was pretty hostile to compiler optimisations, to the point that some PL researchers say its wide adoption set us back decades. It's only later that compilers started to really optimise stuff themselves. And they used undefined behaviour to do so. Signed integer overflow UB for instance was used to optimised `for` loops, thus compensating the verbose, hard to optimise original design. > It's not that the C compiler authors are out to get you The end result is the same. "Oh, this code leads to undefined behaviour. Let's pretend it's dead, it will be faster that way". Apply that to reasonable looking, yet incorrect, safety checks (like checking for integer overflow after the fact), and just like that you have a buffer overflow and an arbitrary code execution exploit.
- pjmlp 9y agoAnd the biggest issue is that the code producing UB might not even be visible to the programmer. It might only visible to the optimizer after applying a few inlining and code transformations before reaching the UB analysis.
- mpweiher 9y ago> It's the compiler's job to make code that is as fast as possible. This is simply wrong, but it absolutely is the misunderstanding that is driving this. It is not the compiler's job to make code that is as fast as possible. If anything, that is the developer's job. The compiler's job is to make the developer's job easier, not to second-guess her.