From 718fd591fa93ff254a9ca68a0ad2ff8a55756489 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Mon, 16 Apr 2012 01:13:25 +0200 Subject: Make sizeof() be of type size_t This matters when sizeof is directly used in arithmetic, ala "uintptr_t t; t &= -sizeof(long)" (for alignment). When sizeof isn't size_t (as it's specified to be) this masking will truncate the high bits of the uintptr_t object (if uintptr_t is larger than uint). --- tests/tcctest.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'tests') diff --git a/tests/tcctest.c b/tests/tcctest.c index eeabb7c..9598fa4 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -2155,6 +2155,8 @@ void c99_vla_test(int size1, int size2) #endif } +typedef __SIZE_TYPE__ uintptr_t; + void sizeof_test(void) { int a; @@ -2175,6 +2177,20 @@ void sizeof_test(void) ptr = NULL; printf("sizeof(**ptr) = %d\n", sizeof (**ptr)); + /* The type of sizeof should be as large as a pointer, actually + it should be size_t. */ + printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int))); + uintptr_t t = 1; + uintptr_t t2; + /* Effectively <<32, but defined also on 32bit machines. */ + t <<= 16; + t <<= 16; + t++; + /* This checks that sizeof really can be used to manipulate + uintptr_t objects, without truncation. */ + t2 = t & -sizeof(uintptr_t); + printf ("%lu %lu\n", t, t2); + /* some alignof tests */ printf("__alignof__(int) = %d\n", __alignof__(int)); printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int)); -- cgit v1.3.1