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

            Bug ID: 119990
           Summary: -Wmaybe-uninitialized -O3 false positive when using
                    std::string in structure and designated initalizer
           Product: gcc
           Version: 15.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: connor24nolan at live dot com
  Target Milestone: ---

Certain structures containing std::strings will trigger a false
-Wmaybe-uninitialized in certain circumstances when compiling with -O3.

This issue also requires -std=c++20 (or higher) or -D_GLIBCXX_USE_CXX11_ABI=0.

Tested:
- GCC Trunk: Has Issue
- GCC 15.1: Has Issue
- GCC 14.2: Works Properly
- GCC 14.2 With -D_GLIBCXX_USE_CXX11_ABI=0: Has Issue
- GCC 12.1 With -D_GLIBCXX_USE_CXX11_ABI=0: Has Issue
- GCC 11.4 With -D_GLIBCXX_USE_CXX11_ABI=0: Works Properly

This does not occur with Clang.

# Code With The Issue (https://godbolt.org/z/MbYxvdEdz):

#include <string>
#include <cstdio>
struct Test2 {
    bool a;
    std::string b;
};
struct Test {
    std::string one;
    Test2 d;
    std::string c;
};
int main() {
    Test obj = {
        .one = "",
        .d = {
            .a = true,
            .b = "hi"
        },
        .c = "hi2"
    };
    printf("%s\n", obj.one.c_str());
}

# Working Code (https://godbolt.org/z/GT68P57fG):

#include <string>
#include <cstdio>
struct Test2 {
    bool a = true;
    std::string b = "hi";
};
struct Test {
    std::string one = "";
    Test2 d;
    std::string c = "hi2";
};
int main() {
    Test obj;
    printf("%s\n", obj.one.c_str());
}

Reply via email to