Re: [PATCH 4/5] sim/erc32: avoid dereferencing type-punned pointer warnings

Andrew Burgess via Gdb-patches
2022-10-24T15:43:12Z

Mike Frysinger <vapier@gentoo.org> writes:

> On 12 Oct 2022 13:38, Andrew Burgess via Gdb-patches wrote:
>> 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 uses an intermediate pointer of type 'char *' when
>> performing the type-punning, which is well-defined behaviour, and will
>> silence the above warning.
>
> this is kind of a poor fix. if we can't arrange for the pointers to be
> the right type, at least a memcpy would be better.

This was already discussed in this thread:

https://sourceware.org/pipermail/gdb-patches/2022-October/192604.html

The version that got pushed:

https://sourceware.org/pipermail/gdb-patches/2022-October/192647.html

does make use of memcpy.

Thanks,
Andrew