From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id 338633858C2D; Wed, 19 Oct 2022 13:33:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 338633858C2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1666186383; bh=cuuNgXsadjhzWSV+c6leOukI79M7xSa83kQO0+IBKlw=; h=From:To:Subject:Date:From; b=EjwWARl+FqUQ2m6YR3bqm+K2o04J9XaCpEActxgy6EpZrH9KrwUKSMNL8pfWXSwhi RWt1anR8CSCEbXZMy3/J+FBGBSHh4wqK5UpNSXuPiNu9a+Ntn0hPGHOkt90h/ORCLR TMUkLhQ6nBCBSF8sbNkcY3BT1OzIoySvZZdnw3xo= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: gdb-cvs@sourceware.org Subject: [binutils-gdb] sim/erc32: avoid dereferencing type-punned pointer warnings X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: e5961d2be503149aaa40927c0a9501c06ca980e5 X-Git-Newrev: d0a7ca87ab208d72b2c6a9d44e30fe20c3c0542e Message-Id: <20221019133303.338633858C2D@sourceware.org> Date: Wed, 19 Oct 2022 13:33:03 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dd0a7ca87ab20= 8d72b2c6a9d44e30fe20c3c0542e commit d0a7ca87ab208d72b2c6a9d44e30fe20c3c0542e Author: Andrew Burgess Date: Wed Oct 12 11:45:53 2022 +0100 sim/erc32: avoid dereferencing type-punned pointer warnings =20 When building the erc32 simulator I get a few warnings like this: =20 /tmp/build/sim/../../src/sim/erc32/exec.c:1377:21: warning: dereferen= cing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasin= g] 1377 | sregs->fs[rd] =3D *((float32 *) & ddata[0]); | ~^~~~~~~~~~~~~~~~~~~~~~~ =20 The type of '& ddata[0]' will be 'uint32_t *', which is what triggers the warning. =20 This commit makes use of memcpy when performing the type-punning, which resolves the above warnings. =20 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. =20 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 =3D clean-sis # behaviour of UART interrupt routines ... SIM_EXTRA_CFLAGS +=3D -DFAST_UART -I$(srcroot) =20 -# Some modules don't build cleanly yet. -exec.o: SIM_WERROR_CFLAGS =3D - ## COMMON_POST_CONFIG_FRAG =20 # `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 =3D TRAP_DEXC; } else { - sregs->fs[rd] =3D *((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 &=3D 0x1E; sregs->flrd =3D rd; - sregs->fs[rd] =3D *((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] =3D *((float32 *) & ddata[1]); + memcpy (&sregs->fs[rd + 1], &ddata[1], + sizeof (sregs->fs[rd + 1])); sregs->ltime =3D ebase.simtime + sregs->icnt + FLSTHOLD + sregs->hold + sregs->fhold; }