public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] sim/erc32: avoid dereferencing type-punned pointer warnings
@ 2022-10-19 13:33 Andrew Burgess
  0 siblings, 0 replies; only message in thread
From: Andrew Burgess @ 2022-10-19 13:33 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d0a7ca87ab208d72b2c6a9d44e30fe20c3c0542e

commit d0a7ca87ab208d72b2c6a9d44e30fe20c3c0542e
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Wed Oct 12 11:45:53 2022 +0100

    sim/erc32: avoid dereferencing type-punned pointer warnings
    
    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.

Diff:
---
 sim/erc32/Makefile.in | 3 ---
 sim/erc32/exec.c      | 7 ++++---
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/sim/erc32/Makefile.in b/sim/erc32/Makefile.in
index 786ae1dcc7b..41830aab726 100644
--- a/sim/erc32/Makefile.in
+++ b/sim/erc32/Makefile.in
@@ -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.
diff --git a/sim/erc32/exec.c b/sim/erc32/exec.c
index ef93692e7a2..26d48c0e46e 100644
--- a/sim/erc32/exec.c
+++ b/sim/erc32/exec.c
@@ -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;
 	    }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-19 13:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-19 13:33 [binutils-gdb] sim/erc32: avoid dereferencing type-punned pointer warnings Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).