Skip to contentSkip to conversation

Can this type-punned warning be silenced by using a union instead of a cast?

PATCH Steve Kenton 1 participant 1 message Dec 28, 2008 Kbuild

lore
Share & tools
lore
#0 lore raw

Can this type-punned warning be silenced by using a union instead of a cast?

HOSTCC scripts/basic/fixdep
scripts/basic/fixdep.c: In function 'traps':
scripts/basic/fixdep.c:377: warning: dereferencing type-punned pointer will break strict-aliasing rules

Something like the attached? As far as I can tell the union should take care of the alignment and make the __attribute__ unnecessary, in which case it does not need to be static either. Compiled with gcc 4.4 cross compiler for i386 successfully but not boot tested.

Steve Kenton

scripts/basic/fixdep.c renamed from scripts/basic/fixdep.c.orig +3 -3
@@ -372,11 +372,11 @@ void print_deps(void)
 
 void traps(void)
 {
-	static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
+	union { int i; char c[4]; } test = { .c = "CONF" };
 
-	if (*(int *)test != INT_CONF) {
+	if (test.i != INT_CONF) {
 		fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianess? %#x\n",
-			*(int *)test);
+			test.i);
 		exit(2);
 	}
 }