Bug#440545: Miscompilation casting signed long to void* in 64bit system
Package: gcc
Version: 4:4.2.1-5
Debian GNU/Linux lenny/sid
amd64
> gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr
--disable-werror --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.2.1 (Debian 4.2.1-4)
Output from attached program, if compiled with optimization level -O2:
>From 0 to 2:
0 - In: -1, -1 (0xffffffffffffffff), Out: -1
1 - In: 0, 0 (0xffffffffffffffff), Out: 0
2 - In: 1, 1 (0xffffffffffffffff), Out: 1
>From 1 to 3:
1 - In: 0, 0 ((nil)), Out: 0
2 - In: 1, 1 (0x1), Out: 1
3 - In: 2, 2 (0x2), Out: 2
Should be
>From 0 to 2:
0 - In: -1, -1 (0xffffffffffffffff), Out: -1
1 - In: 0, 0 ((nil)), Out: 0
2 - In: 1, 1 (0x1), Out: 1
>From 1 to 3:
1 - In: 0, 0 ((nil)), Out: 0
2 - In: 1, 1 (0x1), Out: 1
3 - In: 2, 2 (0x2), Out: 2
This problem hit me when glib GINT_TO_POINTER() returned wrong values.
- ML
#include <assert.h>
#include <stdlib.h>
int main()
{
int i = 0;
printf("From 0 to 2:\n");
for (i = 0; i < 3; i++) {
int iin = i - 1;
signed long lin = iin;
void *pin = (void *) lin;
signed long iout = (signed long) pin;
printf(" %d - In: %d, %d (%p), Out: %d\n", i, iin, lin, pin, iout);
}
printf("From 1 to 3:\n");
for (i = 1; i < 4; i++) {
int iin = i - 1;
signed long lin = iin;
void *pin = (void *) lin;
signed long iout = (signed long) pin;
printf(" %d - In: %d, %d (%p), Out: %d\n", i, iin, lin, pin, iout);
}
return EXIT_SUCCESS;
}
Reply to: