public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111094] New: Spurious Wuninitialized swapping underlying bytes of object representation in move constructor
@ 2023-08-21 19:26 ed at catmur dot uk
  2023-08-21 19:33 ` [Bug tree-optimization/111094] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: ed at catmur dot uk @ 2023-08-21 19:26 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111094
           Summary: Spurious Wuninitialized swapping underlying bytes of
                    object representation in move constructor
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ed at catmur dot uk
  Target Milestone: ---

struct S { short x, y; };
struct A {
    A() {}
    A(A&& rhs) {
        auto p = reinterpret_cast<unsigned char*>(&s);
        auto q = reinterpret_cast<unsigned char*>(&rhs.s);
        for (int i = 0; i != sizeof s; ++i) {
            auto t = p[i];
            p[i] = q[i];
            q[i] = t;
        }
    }
    bool b = false;
    S s;
};
A f() {
    A a;
    A b = static_cast<A&&>(a);
    return b;
}

at -O3 -Wall:

In constructor 'A::A(A&&)',
    inlined from 'A f()' at <source>:18:29:
<source>:9:23: warning: '*(__vector(4) unsigned char*)((char*)&a + offsetof(A,
A::s.S::x))' is used uninitialized [-Wuninitialized]
    9 |             p[i] = q[i];
      |                    ~~~^
<source>: In function 'A f()':
<source>:17:7: note: 'a' declared here
   17 |     A a;
      |       ^

I'm reasonably sure that this usage of swapping underlying bytes is OK by
[basic.indet]/2.3.

(Motivation is e.g. optional with empty-on-move behavior and optimization for
trivial types.)

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

end of thread, other threads:[~2023-08-21 19:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-21 19:26 [Bug c++/111094] New: Spurious Wuninitialized swapping underlying bytes of object representation in move constructor ed at catmur dot uk
2023-08-21 19:33 ` [Bug tree-optimization/111094] " pinskia at gcc dot gnu.org
2023-08-21 19:34 ` pinskia at gcc dot gnu.org
2023-08-21 19:43 ` pinskia at gcc dot gnu.org
2023-08-21 19:59 ` ed at catmur dot uk

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