3 ms·
There's a misleading statement in the "undefined behavior" slide. Allow me to nitpick, since this subject is so full of misunderstandings and confusion. "What
by FartyMcFarter 8y ago
There's a misleading statement in the "undefined behavior" slide. Allow me to nitpick, since this subject is so full of misunderstandings and confusion.
"What are the contents of uninitialized variables?
... whatever was in memory before now!"
This may be true or false - as the slide itself says, "with undefined behavior, anything is possible!".
Besides, the subject of accessing uninitialized variables is more nuanced than "undefined behavior". Among other things, the effects depend on the variable's type ("unsigned char" does not have trap values):
https://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior https://stackoverflow.com/questions/11962457/why-is-using-an...
https://stackoverflow.com/questions/6725809/trap-representation https://stackoverflow.com/questions/6725809/trap-representat...
It's also important to note that C++ has different rules on this. For example, the extract_int function in the last link is valid C, but not valid C++ (in C++ you'd use std::memcpy to achieve the same thing in a valid way).
- throwaway713824 8y agoThis comment, while adding detail, does nothing to improve upon the original statement, which was perfectly valid, and adds nothing to the discussion. This is irrelevant pedantry that a certain stripe of c pedant delights in bludgeoning the rest of us with.
- FartyMcFarter 8y agoHow was the original statement perfectly valid? Let's try with two different compilers: gcc 8.2: https://godbolt.org/z/ZcsChr https://godbolt.org/z/ZcsChr clang 3.8: https://godbolt.org/z/pVDCPi https://godbolt.org/z/pVDCPi It looks like at least gcc disagrees that reading uninitialized variables is a way to find out what's in memory.
- deleted 8y ago[deleted]
- quietbritishjim 8y agoThere is a genuine, non-pedantic difference between a variable having an arbitrary value and reading it causing undefined behaviour. For example, say you don't care what value a variable has so long as it's even: void foo() { int i; i -= i % 2; printf("%d %d\n", i, i); } The numbers printed could be odd, because the compiler is allowed to do anything it likes. It could even print two different numbers out! If you still think this is all pedantry that can't happen in practice, here's an example where compilers are known to do strange things when the behaviour is undefined: int bar(int param) { int uninit; if (param == 0) { uninit = 0; } baz(param); // Some other function return uninit; } In this snippet of code, you might think that you're safe so long as you don't look at the return value of baz(). But in fact the optimiser may conclude that param must be zero (because anything else would be undefined behaviour), so baz() is always called with a parameter of zero even if bar has another argument. An problem very similar to this was discussed as a source of possible vulnerabilities in the Linux kernel [1] (although I don't know if any actual vulnerabilities were found). [1] https://lwn.net/Articles/575563/ https://lwn.net/Articles/575563/
- throwaway713824 8y ago"with undefined behavior, anything is possible!". How is your exposition not a subsumed by that statement? C pedants? Right we get it, nobody needs the excruciating details you and your kind delight in. It's noise.
- pjmlp 8y agoAlso to note that ISO C++ working group is trying to reduce the amount of UB, while ISO C working group doesn't have any ongoing papers into this direction.