public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional
@ 2020-05-02 13:46 dawid_jurek at vp dot pl
  2020-05-02 15:38 ` [Bug c++/94923] " daniel.kruegler at googlemail dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: dawid_jurek at vp dot pl @ 2020-05-02 13:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94923
           Summary: False positive -Wclass-memaccess with trivially
                    copyable std::optional
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dawid_jurek at vp dot pl
  Target Milestone: ---

Consider following C++ snippet:

static_assert(std::is_trivially_copyable_v<std::optional<char>>);
static void not_ok() {
  std::optional<char> value;
  std::byte buf[128;
  std::memcpy(&buf[0], &value, sizeof value);
  std::memcpy(&value, &buf[0], sizeof value);
}

With above snippet fresh x86-64 gcc trunk reports following warning:

source>: In function 'void not_ok()':

<source>:27:46: warning: 'void* memcpy(void*, const void*, size_t)' copying an
object of non-trivial type 'class std::optional<char>' from an array of 'enum
class std::byte' [-Wclass-memaccess]

   27 |     std::memcpy(&value, &buf[0], sizeof value);

[basic.types]/2 explicitly permits for such round trip for every trivially
copyable T via std::memcpy even if T is not trivial.
Also clang accept such code without complains (take a look at link below).
Changing std::byte buf[128; to char buf[128]; suppress the warning so seems
that std::byte is relevant.
You can find full source code here https://godbolt.org/z/JrcnS5

Regards,
Dawid

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

* [Bug c++/94923] False positive -Wclass-memaccess with trivially copyable std::optional
  2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
@ 2020-05-02 15:38 ` daniel.kruegler at googlemail dot com
  2020-05-02 17:59 ` dawid_jurek at vp dot pl
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2020-05-02 15:38 UTC (permalink / raw)
  To: gcc-bugs

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

Daniel Krügler <daniel.kruegler at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.kruegler@googlemail.
                   |                            |com

--- Comment #1 from Daniel Krügler <daniel.kruegler at googlemail dot com> ---
Corrected code without typos and including the necessary header files:

```
#include <type_traits>
#include <optional>
#include <cstddef>
#include <cstring>

static_assert(std::is_trivially_copyable_v<std::optional<char>>);

void not_ok() {
  std::optional<char> value;
  std::byte buf[128];
  std::memcpy(&buf[0], &value, sizeof value);
  std::memcpy(&value, &buf[0], sizeof value);
}

int main() {}
```

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

* [Bug c++/94923] False positive -Wclass-memaccess with trivially copyable std::optional
  2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
  2020-05-02 15:38 ` [Bug c++/94923] " daniel.kruegler at googlemail dot com
@ 2020-05-02 17:59 ` dawid_jurek at vp dot pl
  2020-05-02 22:34 ` msebor at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: dawid_jurek at vp dot pl @ 2020-05-02 17:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from dawid_jurek at vp dot pl ---
Yeah, that's the correct reproducer of issue I reported. 
I noticed missing bracket after pasting it from godbolt but unfortunately
couldn't edit my comment after posting.

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

* [Bug c++/94923] False positive -Wclass-memaccess with trivially copyable std::optional
  2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
  2020-05-02 15:38 ` [Bug c++/94923] " daniel.kruegler at googlemail dot com
  2020-05-02 17:59 ` dawid_jurek at vp dot pl
@ 2020-05-02 22:34 ` msebor at gcc dot gnu.org
  2020-05-02 22:36 ` msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-05-02 22:34 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-05-02
     Ever confirmed|0                           |1
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning code treats character types as special in this context but there's
no such handling for std::byte.  It probably makes sense to extend the same
treatment to it.

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

* [Bug c++/94923] False positive -Wclass-memaccess with trivially copyable std::optional
  2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
                   ` (2 preceding siblings ...)
  2020-05-02 22:34 ` msebor at gcc dot gnu.org
@ 2020-05-02 22:36 ` msebor at gcc dot gnu.org
  2020-05-08 18:58 ` msebor at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-05-02 22:36 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |10.0, 8.4.0, 9.3.0
           Assignee|unassigned at gcc dot gnu.org      |msebor at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

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

* [Bug c++/94923] False positive -Wclass-memaccess with trivially copyable std::optional
  2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
                   ` (3 preceding siblings ...)
  2020-05-02 22:36 ` msebor at gcc dot gnu.org
@ 2020-05-08 18:58 ` msebor at gcc dot gnu.org
  2020-05-19 19:09 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-05-08 18:58 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
Patch: https://gcc.gnu.org/pipermail/gcc-patches/2020-May/545421.html

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

* [Bug c++/94923] False positive -Wclass-memaccess with trivially copyable std::optional
  2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
                   ` (4 preceding siblings ...)
  2020-05-08 18:58 ` msebor at gcc dot gnu.org
@ 2020-05-19 19:09 ` cvs-commit at gcc dot gnu.org
  2020-05-21  8:31 ` dawid_jurek at vp dot pl
  2022-03-17 19:53 ` msebor at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-05-19 19:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:c0d8623ce5aa6d92c2e6c62e1bee66272a011f59

commit r11-499-gc0d8623ce5aa6d92c2e6c62e1bee66272a011f59
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue May 19 12:46:37 2020 -0600

    PR c++/94923 - False positive -Wclass-memaccess with trivially copyable
std::optional

    gcc/cp/ChangeLog:

            PR c++/94923
            * call.c ((maybe_warn_class_memaccess): Use is_byte_access_type.
            * cp-tree.h (is_dummy_object): Return bool.
            (is_byte_access_type): Declare new function.
            * tree.c (is_dummy_object): Return bool.
            (is_byte_access_type): Define new function.

    gcc/testsuite/ChangeLog:

            PR c++/94923
            * g++.dg/Wclass-memaccess.C: Add tests for std::byte.

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

* [Bug c++/94923] False positive -Wclass-memaccess with trivially copyable std::optional
  2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
                   ` (5 preceding siblings ...)
  2020-05-19 19:09 ` cvs-commit at gcc dot gnu.org
@ 2020-05-21  8:31 ` dawid_jurek at vp dot pl
  2022-03-17 19:53 ` msebor at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: dawid_jurek at vp dot pl @ 2020-05-21  8:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from dawid_jurek at vp dot pl ---
I quickly tried trunk and all false positive warnings vanished. 
Thanks Martin.

Regards,
Dawid

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

* [Bug c++/94923] False positive -Wclass-memaccess with trivially copyable std::optional
  2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
                   ` (6 preceding siblings ...)
  2020-05-21  8:31 ` dawid_jurek at vp dot pl
@ 2022-03-17 19:53 ` msebor at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-03-17 19:53 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #7 from Martin Sebor <msebor at gcc dot gnu.org> ---
Fixed per comment #6.

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

end of thread, other threads:[~2022-03-17 19:53 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-02 13:46 [Bug c++/94923] New: False positive -Wclass-memaccess with trivially copyable std::optional dawid_jurek at vp dot pl
2020-05-02 15:38 ` [Bug c++/94923] " daniel.kruegler at googlemail dot com
2020-05-02 17:59 ` dawid_jurek at vp dot pl
2020-05-02 22:34 ` msebor at gcc dot gnu.org
2020-05-02 22:36 ` msebor at gcc dot gnu.org
2020-05-08 18:58 ` msebor at gcc dot gnu.org
2020-05-19 19:09 ` cvs-commit at gcc dot gnu.org
2020-05-21  8:31 ` dawid_jurek at vp dot pl
2022-03-17 19:53 ` msebor 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).