[PATCH 06/11] Sptr: avoided potentially undefined behaviour.

Andrew Clayton andrew at digital-domain.net
Thu Jun 16 15:03:38 UTC 2022


On Thu, 16 Jun 2022 13:35:12 +0300
Max Romanov <max.romanov at gmail.com> wrote:

> Hello,

Thanks for the review!
 
> This patch is useless. Please try to understand the structure of sptr_t before made such changes.

Yes, that union is a bit of a head scratcher! ;)

> How extra assignment to temporary variable may change undefined behavior to defined?

The idea was to avoid undefined memory ordering.

> This change was made just to shut the cppcheck which warning is a false positive.

So it wasn't just to shut up cppcheck, I already filtered out many
false positives.

This one was a bit tricky to nail down but between SO[0], the
C(11) standard[1] and cppcheck I came down on the side of it being UB,
due to thinking that the ordering of the read and write of the two union
members was UB.

So if we're sure that it is indeed OK, then this can simply be dropped.
No problem.

Cheers,
Andrew

[0]:
https://stackoverflow.com/questions/33291146/c-c-unions-and-undefined-behaviour/33291213#33291213
[1]: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf



More information about the unit mailing list