Created attachment 53044 [details] t.ii reproducer Compiling following with -Wall -Werror -Os produces bogus maybe-unitialized warning /tmp/t.cpp: In function ‘void t(const string&)’: /tmp/t.cpp:12:51: error: ‘*(int*)((char*)&port + offsetof(std::optional<int>,std::optional<int>::<unnamed>.std::_Optional_base<int, true, true>::<unnamed>))’ may be used uninitialized in this function [-Werror=maybe-uninitialized] 12 | reqb << "Host: " << port.value_or(443) << "\r\n"; Attaching .ii (tar.gz due to attachment limit) file from 11.2.0, but it is also reproducible on trunk in godbolt: https://godbolt.org/z/ha3vn61n9 Interesting that commenting unrelated parts or changing them slightly makes warning go away. #include <regex> #include <optional> #include <sstream> void t(const std::string& i) { const std::regex re("^$"); //removing this works std::optional<int> port(i.length() > 0 ? std::make_optional(std::strtol("99", nullptr, 10)) : std::nullopt); std::ostringstream reqb; reqb << "GET /" << i << " HTTP/1.1" << "\r\n"; //removing this works reqb << "Host: " << port.value_or(443) << "\r\n"; }
>Interesting that commenting unrelated parts or changing them slightly makes warning go away. Most likely inlining changes with those being there vs not being there.
<source>: In function 'void t(const std::string&)': <source>:13:51: warning: '*(int*)((char*)&port + offsetof(std::optional<int>,std::optional<int>::<unnamed>.std::_Optional_base<int, true, true>::<unnamed>))' may be used uninitialized [-Wmaybe-uninitialized] 13 | reqb << "Host: " << port.value_or(443) << "\r\n"; | ^~~~~~ <source>:9:28: note: '*(int*)((char*)&port + offsetof(std::optional<int>,std::optional<int>::<unnamed>.std::_Optional_base<int, true, true>::<unnamed>))' was declared here 9 | std::optional<int> port(i.length() > 0 ? std::make_optional(std::strtol("99", nullptr, 10)) : std::nullopt); | ^~~~
Note in GCC 10.3.0 we got: <source>: In function 'void t(const string&)': <source>:13:51: warning: 'port' may be used uninitialized in this function [-Wmaybe-uninitialized] 13 | reqb << "Host: " << port.value_or(443) << "\r\n"; | ^~~~~~
Confirmed on the GCC 12 branch but on master the diagnostic only shows with -O1 now ...