4 ms·
There 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
by quietbritishjim 8y ago
There 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.