public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113111] New: -Werror=uninitialized is not consistent for optimization level 0 or -std=before-c++20
@ 2023-12-22  8:15 MikeSmith32564 at mail dot com
  2024-01-18 19:23 ` [Bug c++/113111] " mpolacek at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: MikeSmith32564 at mail dot com @ 2023-12-22  8:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113111
           Summary: -Werror=uninitialized is not consistent for
                    optimization level 0 or -std=before-c++20
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: MikeSmith32564 at mail dot com
  Target Milestone: ---

The following snippet:



#include <string>
#include <iostream>

struct Dummy
{
    std::string val;
};

int main()
{
    Dummy d =
    {
        d.val = "random text
#########################################################"
    };

    std::cout<<d.val<<"\n";
}



when built with:
g++ -std=c++20 -O0 -Wall -Werror main.cpp -o a.out
should generate a warning a.k.a. compilation error due to -Werror about trying
to assign from an uninitialized variable. When -O1, -O2 or -O3 are used the
expected warning is shown:

include/c++/14.0.0/bits/basic_string.h:1084:16: error:
'd.Dummy::val.std::__cxx11::basic_string<char>::_M_string_length' is used
uninitialized [-Werror=uninitialized]
 1084 |       { return _M_string_length; }
      |                ^~~~~~~~~~~~~~~~

The example triggers undefined behavior when executed of course due to running
std::string::operator= with the this pointer pointing to uninitialized memory
but I believe a warning should be raised when building regardless of the
optimization level selected.

Strangely, standards before c++20 also do not report the warning.

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

* [Bug c++/113111] -Werror=uninitialized is not consistent for optimization level 0 or -std=before-c++20
  2023-12-22  8:15 [Bug c++/113111] New: -Werror=uninitialized is not consistent for optimization level 0 or -std=before-c++20 MikeSmith32564 at mail dot com
@ 2024-01-18 19:23 ` mpolacek at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-01-18 19:23 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
                 CC|                            |mpolacek at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-01-18

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Confirmed, if for nothing else then for our ugly diagnostic.  clang++ says:

113111.C:13:9: warning: variable 'd' is uninitialized when used within its own
initialization [-Wuninitialized]
   11 |     Dummy d =
      |           ~
   12 |     {
   13 |         d.val = "random text
#########################################################"
      |         ^

Much better than a wall of text showing internal code...  :/

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

end of thread, other threads:[~2024-01-18 19:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-22  8:15 [Bug c++/113111] New: -Werror=uninitialized is not consistent for optimization level 0 or -std=before-c++20 MikeSmith32564 at mail dot com
2024-01-18 19:23 ` [Bug c++/113111] " mpolacek 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).