sim/erc32: avoid dereferencing type-punned pointer warnings
authorAndrew Burgess <aburgess@redhat.com>
Wed, 12 Oct 2022 10:45:53 +0000 (11:45 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Wed, 19 Oct 2022 13:32:22 +0000 (14:32 +0100)
When building the erc32 simulator I get a few warnings like this:

  /tmp/build/sim/../../src/sim/erc32/exec.c:1377:21: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   1377 |   sregs->fs[rd] = *((float32 *) & ddata[0]);
        |                    ~^~~~~~~~~~~~~~~~~~~~~~~

The type of '& ddata[0]' will be 'uint32_t *', which is what triggers
the warning.

This commit makes use of memcpy when performing the type-punning,
which resolves the above warnings.

With this change, I now see no warnings when compiling exec.c, which
means that the line in Makefile.in that disables -Werror can be
removed.

There should be no change in behaviour after this commit.

sim/erc32/Makefile.in
sim/erc32/exec.c

index 786ae1dcc7b58ac44967f3084598008a601e6ef7..41830aab726ff4fe3e03c33df225d43cac9651ff 100644 (file)
@@ -32,9 +32,6 @@ SIM_EXTRA_CLEAN = clean-sis
 # behaviour of UART interrupt routines ...
 SIM_EXTRA_CFLAGS += -DFAST_UART -I$(srcroot)
 
-# Some modules don't build cleanly yet.
-exec.o: SIM_WERROR_CFLAGS =
-
 ## COMMON_POST_CONFIG_FRAG
 
 # `sis' doesn't need interf.o.
index ef93692e7a26cc35f69baa53a5a6f0a241d0dc07..26d48c0e46effcfbf6b4d886e546241ed75d3e33 100644 (file)
@@ -1345,7 +1345,7 @@ dispatch_instruction(struct pstate *sregs)
            if (mexc) {
                sregs->trap = TRAP_DEXC;
            } else {
-               sregs->fs[rd] = *((float32 *) & data);
+               memcpy (&sregs->fs[rd], &data, sizeof (sregs->fs[rd]));
            }
            break;
        case LDDF:
@@ -1373,11 +1373,12 @@ dispatch_instruction(struct pstate *sregs)
            } else {
                rd &= 0x1E;
                sregs->flrd = rd;
-               sregs->fs[rd] = *((float32 *) & ddata[0]);
+               memcpy (&sregs->fs[rd], &ddata[0], sizeof (sregs->fs[rd]));
 #ifdef STAT
                sregs->nload++; /* Double load counts twice */
 #endif
-               sregs->fs[rd + 1] = *((float32 *) & ddata[1]);
+               memcpy (&sregs->fs[rd + 1], &ddata[1],
+                       sizeof (sregs->fs[rd + 1]));
                sregs->ltime = ebase.simtime + sregs->icnt + FLSTHOLD +
                               sregs->hold + sregs->fhold;
            }