undefined behaviour
Posted Jul 17, 2009 15:55 UTC (Fri) by
forthy (guest, #1525)
In reply to:
undefined behaviour by bluebirch
Parent article:
Linux 2.6.30 exploit posted
The question is "is that reasonable"? And what is "undefined behavior"?
The GCC maintainer (the language talibans) argue that whatever the C99
standard defines not accurately (and that's pretty much all of the it ;-)
is left open "undefined". I suppose that if x+n >= x holds true, than x+n
really ought to be greater than x - which is not true when compiled
by GCC. Same for dereferencing the NULL pointer: This is not defined in C,
but being not defined does not say "it breaks". When it may not break,
optimizing the test away is simply wrong. If GCC's optimizer would work a
bit differently, it would reorder the access and the test - because the
test causes the function to return without using the accessed field (this
value is dead, and dead loads can be optimized away) - and then by the
funny logic the test is both vital and can be optimized away ;-).
Not.
But I've basically given up on the GCC maintainer to do reasonable
things, anyway. There interpretation of standards is just upside down: A
standard is a compromise between various implementers and users, so that
different qualities of implementations can claim they are "standard". It's
like the POSIX discussion we had some times ago here (ext4 problems) where
POSIX allowed a file system to loose data and leave the filesystem in a
limbo state. This is not a good implementation if it does so.
Standard writers can drive towards good implementations while still
allowing bad ones: Use the word "should" instead of "shall". Like "a file
system should preserve a consistent state in case of a crash" - this means
"best effort", and the amount of reasonable effort is left to the
implementer. A C compiler should wrap around numbers on a two's complement
system, not trap, not crash. The code should honor the execution model of
the underlying machine (which e.g. can dereference null pointers). And
making a language defined only in vague terms is not a good idea - because
writing programs in an underspecified language won't work. The "good"
practice of implementing such an underspecified language is to define the
terms properly and then stick to them - and hope the practice catches on,
so that the next round of standardization effort can encode them with
"shall" instead of "should".
(
Log in to post comments)