2018-01-15 07:58:29 +00:00

79 lines
2.4 KiB
C

/*
ANSI C cheat.
This file is being split up into smaller parts and will eventually disappear.
*/
#include "common.h"
int main() {
/*
# Overflow
Overflow has two meanings:
- mathemtaical definition: making an operation in which the result
is larger than the maximum value or smaller than the minimum value.
This is the more common meaning of the term *overflow* outside of this context.
In this section this will be called *mathematical overflow*.
There is no automatic overflow check on operations
except at initialization and assignment by constants
at compile time.
Operations that can overflow mathematically but never overflow include:
- unsigned integer sum or multiplication
- flag definition: raising the overflow FLAG on a x86 processor or analogous.
This is the meaning of overflow in the ANSI C definition.
Therefore, in this section this will be called simply *overflow*.
Overflow always implies undefined behaviour. This could include a program crash!
Operations that may overflow include:
- signed integer sum or multiplication
Since this kind of overflow modifies an observable processor state,
it is possible for compilers to detect this situation.
Flag overflow always implies mathematical overflow, but not the converse.
For instance, unsigned integer overflow never raises the overflow flag of an x86
processor: it just wraps around.
Therefore, unsigned integer multiplication never overflows.
# GCC overflow specifics
gcc 4.8 is smart enough to warn in many cases with `-Woverflow`:
which is part of `-Wall`. We have turned it off so it wont annoy us on our tests.
`-ftrapv` causes gcc to detect an overflow and raise a `SIGABRT`,
this making their behaviour defined.
*/
{
/*
Initialize with literal that does not fit into type
TODO what happens?
*/
{
if (0) {
/*unsigned char uc = UCHAR_MAX + 1;*/
/*char c = 1e1000;*/
}
/* Floating point */
{
assert(1.00000000000000000000000000000000000000000000001 == 1.0);
}
}
}
}