pure64 & -ftrapv
Hi all,
I'm enjoying Debian pure64. It's astonishing how many applications have
been correctly ported. Many thanks for all the fantastic work!
#include <stdio.h>
#include <stdint.h>
int32_t a=0x7FFFFFFF;
int main(void) {
printf("a+1=%i\n", a+1);
return 0;
}
The above code compiled with gcc-3.3, gcc-3.4 and gcc-4.0 (pulled in from
gcc-3.4 experimental) and the -ftrapv option prints a+1=-214748364 each
time. It should abort.
$ dpkg -l | grep gcc-
ii gcc-3.3 3.3.5-6 The GNU C compiler
ii gcc-3.3-base 3.3.5-6 The GNU Compiler Collection (base package)
ii gcc-3.4 3.4.3-7 The GNU C compiler
ii gcc-3.4-base 3.4.3-7 The GNU Compiler Collection (base package)
ii gcc-4.0 4.0-0pre2 The GNU C compiler
ii gcc-4.0-base 4.0-0pre2 The GNU Compiler Collection (base package)
It appears I've created another example of "-ftrapv borks up simple
integer arithmetic": <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18665>
Does the above bugzilla entry mean I shouldn't submit a bug report to gcc
developers?
Once this works I going to attempt catching signed integer overflow to
promote dynamically typed signed 32-bit integer arithmetic to signed
64-bit.
A concern is that trapping signed overflow may break use of ptrdiff_t on
32-bit platforms where user address space is larger than 2GB. For example,
consider a vector V of pointers to objects O where each O contains
pointers back to V. If V is realloc|ated the pointers from O to V will
usually have to be fixed up by adding the offset of the new address of V
from the old address of V to each pointer in O.
If V started low in the address space, say 0x10000000, and when
reallocated now started at 0xB0000000, then the offset would be
0xA0000000; which does not fit into a signed 32 bit integer (so the
program would abort).
It appears I should first perform a pointer comparison and then branch to
using unsigned integer arithmetic (which branch depends upon whether the
new address is larger than the old address or vice versa).
I note that the outcome of signed integer overflow is already undefined
in portable C (as well as pointer arithmetic between different objects).
Regards,
Adam
Reply to: