public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jorgen.lind at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/94951] New: dereferencing type-punned pointer will break strict-aliasing rules when using super class for a template type
Date: Tue, 05 May 2020 10:01:17 +0000	[thread overview]
Message-ID: <bug-94951-4@http.gcc.gnu.org/bugzilla/> (raw)

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;
    }
};

             reply	other threads:[~2020-05-05 10:01 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-05 10:01 jorgen.lind at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-94951-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).