public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94951] New: dereferencing type-punned pointer will break strict-aliasing rules when using super class for a template type
@ 2020-05-05 10:01 jorgen.lind at gmail dot com
  2020-05-05 11:47 ` [Bug c++/94951] " rguenth at gcc dot gnu.org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: jorgen.lind at gmail dot com @ 2020-05-05 10:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94951

            Bug ID: 94951
           Summary: dereferencing type-punned pointer will break
                    strict-aliasing rules when using super class for a
                    template type
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jorgen.lind at gmail dot com
  Target Milestone: ---

The following program issues the warning when compiled with -O2 or -O3.

The workaround is to static_cast usage of members to super class.
In the godbolt example its sufficient to add brackets around the class instance
before member access, but was not sufficient in a more complex example. 

The behavior is present from 7.1 until trunk.
Godbolt link:https://godbolt.org/z/Kfp-ac

#include <stdint.h>

struct A
{
    uint32_t someData[32];
};

template<int N>
struct B : public A
{
    static B<N> createB(uint32_t (&otherData)[32])
    {
        B<N> toReturn;
        toReturn.someData[2] = 4;
        return toReturn;
    }
};

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2021-10-25  5:08 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-05 10:01 [Bug c++/94951] New: dereferencing type-punned pointer will break strict-aliasing rules when using super class for a template type jorgen.lind at gmail dot com
2020-05-05 11:47 ` [Bug c++/94951] " rguenth at gcc dot gnu.org
2020-05-05 13:52 ` [Bug c++/94951] [8/9/10/11 Regression] " jakub at gcc dot gnu.org
2020-05-05 13:54 ` jakub at gcc dot gnu.org
2020-05-05 13:56 ` jakub at gcc dot gnu.org
2020-05-05 13:56 ` jakub at gcc dot gnu.org
2020-05-05 16:28 ` jakub at gcc dot gnu.org
2020-05-06 21:38 ` cvs-commit at gcc dot gnu.org
2020-05-07 13:28 ` cvs-commit at gcc dot gnu.org
2020-05-07 13:36 ` [Bug c++/94951] [8/9 " jakub at gcc dot gnu.org
2020-09-16 19:22 ` cvs-commit at gcc dot gnu.org
2020-09-17 17:50 ` jakub at gcc dot gnu.org
2021-07-23 21:19 ` pinskia at gcc dot gnu.org
2021-10-25  5:08 ` pinskia at gcc dot gnu.org

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).