From f2db78a346112f5320efc6f0b6b9fe4ae0d893d3 Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Mon, 22 Mar 2010 15:00:39 -0700 Subject: [PATCH] Fix strict aliasing problems or add -fno-strict-aliasing Create a new CFLAGS_NOSTRICT Autoconf substitution variable which is set to -fno-strict-aliasing if we're using gcc to compile. Add @CFLAGS_NOSTRICT@ to the C compiler flags for source files that have significant strict-aliasing issues. Fix minor strict-aliasing issues elsewhere in the tree by using memcpy to copy the data into a properly typed variable. Change-Id: I43afdb5e33e06cb249b7db385a65daf8d2133a19 Reviewed-on: http://gerrit.openafs.org/1617 Reviewed-by: Derrick Brashear Tested-by: Derrick Brashear --- src/budb/ol_verify.c | 4 +++- src/budb/procs.c | 15 +++++++++++---- src/cf/osconf.m4 | 7 +++++++ src/kauth/krb_udp.c | 3 ++- src/ptserver/Makefile.in | 4 ++++ src/ptserver/ptclient.c | 4 +++- src/tsalvaged/Makefile.in | 3 ++- src/vol/Makefile.in | 4 ++++ 8 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/budb/ol_verify.c b/src/budb/ol_verify.c index 5a57e9e..0ca22a5 100644 --- a/src/budb/ol_verify.c +++ b/src/budb/ol_verify.c @@ -935,6 +935,7 @@ verifyEntryChains(struct ubik_trans *ut) { afs_int32 code; afs_int32 offset; + afs_int32 start; int blockIndex, entryIndex; char entry[sizeof(struct block)]; int entrySize; @@ -966,7 +967,8 @@ verifyEntryChains(struct ubik_trans *ut) return BUDB_IO; /* check if entry is free by looking at the first "afs_int32" of the structure */ - if (*((afs_int32 *) & entry[0]) == 0) { /* zero is free */ + memcpy(&start, entry, sizeof(start)); + if (start == 0) { /* zero is free */ /* Is it on any hash chain? */ if (blockMap[blockIndex]->entries[entryIndex] & MAP_HASHES) { Log("Entry: blockindex %d, entryindex %d - marked free but hashed 0x%x\n", blockIndex, entryIndex, blockMap[blockIndex]->entries[entryIndex]); diff --git a/src/budb/procs.c b/src/budb/procs.c index 34eb55c..1ef235f 100644 --- a/src/budb/procs.c +++ b/src/budb/procs.c @@ -3405,6 +3405,9 @@ T_DumpHashTable(struct rx_call *call, int type, char *filename) afs_uint32 hash; dbadr a, first_a; char e[sizeof(struct block)]; /* unnecessarily conservative */ + struct dump e_dump; + struct tape e_tape; + struct volInfo e_volinfo; int e_size; int old; @@ -3448,16 +3451,20 @@ T_DumpHashTable(struct rx_call *call, int type, char *filename) fprintf(DUMP, " at %d is ", a); switch (type) { case HT_dumpIden_FUNCTION: - fprintf(DUMP, "%d\n", ntohl(((struct dump *)e)->id)); + memcpy(&e_dump, e, sizeof(e_dump)); + fprintf(DUMP, "%d\n", ntohl(e_dump.id)); break; case HT_dumpName_FUNCTION: - fprintf(DUMP, "%s\n", ((struct dump *)e)->dumpName); + memcpy(&e_dump, e, sizeof(e_dump)); + fprintf(DUMP, "%s\n", e_dump.dumpName); break; case HT_tapeName_FUNCTION: - fprintf(DUMP, "%s\n", ((struct tape *)e)->name); + memcpy(&e_tape, e, sizeof(e_tape)); + fprintf(DUMP, "%s\n", e_tape.name); break; case HT_volName_FUNCTION: - fprintf(DUMP, "%s\n", ((struct volInfo *)e)->name); + memcpy(&e_volinfo, e, sizeof(e_volinfo)); + fprintf(DUMP, "%s\n", e_volinfo.name); break; } if ((ht_HashEntry(mht, e) % length) != hash) diff --git a/src/cf/osconf.m4 b/src/cf/osconf.m4 index 2622cf7..7d6be57 100644 --- a/src/cf/osconf.m4 +++ b/src/cf/osconf.m4 @@ -1135,6 +1135,12 @@ if test "x$GCC" = "xyes"; then fi fi +CFLAGS_NOSTRICT= + +if test "x$GCC" = "xyes"; then + CFLAGS_NOSTRICT="-fno-strict-aliasing" +fi + if test "x$GCC" = "xyes"; then CFLAGS_NOUNUSED="-Wno-unused" else @@ -1191,6 +1197,7 @@ AC_SUBST(TXLIBS) AC_SUBST(VFSCK_CFLAGS) AC_SUBST(XCFLAGS) AC_SUBST(CFLAGS_NOERROR) +AC_SUBST(CFLAGS_NOSTRICT) AC_SUBST(CFLAGS_NOUNUSED) AC_SUBST(XCFLAGS64) AC_SUBST(XLDFLAGS) diff --git a/src/kauth/krb_udp.c b/src/kauth/krb_udp.c index 7d516cf..9f4f364 100644 --- a/src/kauth/krb_udp.c +++ b/src/kauth/krb_udp.c @@ -307,7 +307,8 @@ UDP_Authenticate(int ksoc, struct sockaddr_in *client, char *name, afs_uint32 temp; /* unsigned for safety */ afs_uint32 pwexpires; - temp = ntohl(*((afs_int32 *) (tentry.misc_auth_bytes))); + memcpy(&temp, tentry.misc_auth_bytes, sizeof(afs_uint32)); + temp = ntohl(temp); unpack_long(temp, misc_auth_bytes); pwexpires = misc_auth_bytes[0]; if (pwexpires) { diff --git a/src/ptserver/Makefile.in b/src/ptserver/Makefile.in index a55177a..aab2fee 100644 --- a/src/ptserver/Makefile.in +++ b/src/ptserver/Makefile.in @@ -95,9 +95,13 @@ ptserver: ptserver.o ptutils.o ptprocs.o ptint.ss.o ptint.xdr.o utils.o $(LIBS) ptserver.o: ptserver.c ${INCLS} AFS_component_version_number.c +# When supergroups is enabled, some parts of ptserver cast between +# incompatible structs. ptutils.o: ptutils.c ${INCLS} + $(CCOBJ) $(CFLAGS) @CFLAGS_NOSTRICT@ -c ptutils.c ptprocs.o: ptprocs.c ${INCLS} + $(CCOBJ) $(CFLAGS) @CFLAGS_NOSTRICT@ -c ptprocs.c utils.o: utils.c ${INCLS} diff --git a/src/ptserver/ptclient.c b/src/ptserver/ptclient.c index cacf8c7..a62b579 100644 --- a/src/ptserver/ptclient.c +++ b/src/ptserver/ptclient.c @@ -133,7 +133,9 @@ PrintEntry(afs_int32 ea, struct prentry *e, int indent) { /* handle screwed up versions of DumpEntry */ if (e->flags & PRCONT) { - afs_int32 id = *(afs_int32 *) (e->name); + afs_int32 id; + + memcpy(&id, e->name, sizeof(id)); if ((id != PRBADID) && ((id > (1 << 24)) || (id < -(1 << 24)))) { /* assume server incorrectly swapped these bytes... */ int i = 0; diff --git a/src/tsalvaged/Makefile.in b/src/tsalvaged/Makefile.in index 464b6ce..c1ea332 100644 --- a/src/tsalvaged/Makefile.in +++ b/src/tsalvaged/Makefile.in @@ -62,8 +62,9 @@ vol-salvage.o: ${VOL}/vol-salvage.c physio.o: ${VOL}/physio.c ${CCRULE} +# Uses an alignment hack to convert between incompatible pointers. fssync-debug.o: ${VOL}/fssync-debug.c - ${CCRULE} + $(CCOBJ) $(CFLAGS) @CFLAGS_NOSTRICT@ -c ${VOL}/fssync-debug.c salvsync-debug.o: salvsync-debug.c ${CCRULE} diff --git a/src/vol/Makefile.in b/src/vol/Makefile.in index 1afeabd..a46f528 100644 --- a/src/vol/Makefile.in +++ b/src/vol/Makefile.in @@ -225,6 +225,10 @@ volinfo: vol-info.o physio.o ihandle.o ${LIBS} ${CC} ${CFLAGS} -o volinfo vol-info.o physio.o \ ihandle.o ${LIBS} ${XLIBS} +# Uses an alignment hack to convert between incompatible pointers. +fssync-debug.o: fssync-debug.c + $(CCOBJ) $(CFLAGS) @CFLAGS_NOSTRICT@ -c fssync-debug.c + fssync-debug: fssync-debug.o physio.o AFS_component_version_number.c ${LIBS} ${CC} ${LDFLAGS} -o fssync-debug fssync-debug.o physio.o ${LIBS} ${XLIBS} -- 1.7.1