32-bit truncation of 64-bit values

Dieter freebsd at sopwith.solgatos.com
Sat Feb 28 21:35:14 PST 2009


> Having just tracked down an issue caused by a pointer-to-int truncation,

Was this in C?  Didn't the compiler complain?

cat demo_ptr_into_int.c ; gcc -O3 -o demo_ptr_into_int demo_ptr_into_int.c ; ./demo_ptr_into_int
/* demo_ptr_into_int.c
 *
 * Verify that compiler complains about truncation when
 * attempting to stuff a 64 bit pointer into a 32 bit int.
 */

#include <stdio.h>   /* for printf(3) */

int
main(int argc, char **argv)
{
  int i;
  int *p;

  p = &i;
  i = p;
  printf("i = 0x%x p = 0x%p\n", i, p);
  return(0);
}
demo_ptr_into_int.c: In function 'main':
demo_ptr_into_int.c:16: warning: assignment makes integer from pointer without a cast
i = 0xffffebc4 p = 0x0x7fffffffebc4


Adding a cast still yields a complaint:

i = (int) p;

demo_ptr_into_int.c: In function 'main':
demo_ptr_into_int.c:16: warning: cast from pointer to integer of different size


More information about the freebsd-amd64 mailing list