https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108554
Bug ID: 108554 Summary: Warning "null pointer dereferece" raised when extracting a unique_ptr from a map and any "-O" flag Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: fcontact at cuveland dot de Target Milestone: --- I found what I believe might be a false positive with g++ 12 when using the "-Wnull-dereference" warning and a map of unique_ptr with any level of optimisation. This behavior does not show up in GCC 9, 10, and 11. Small but complete example: #include <map> #include <memory> #include <string> #include <utility> int main() { // create and initialize a map std::map<std::string, std::unique_ptr<int>> my_map; my_map["my_key"] = std::make_unique<int>(1); auto it = my_map.find("my_key"); if (it != my_map.end()) { // extract an item from the map auto item = std::move(my_map.extract(it).mapped()); return *item; } return 0; } Compiled with: g++-12 -O -Wnull-dereference <filename> Compiler version: g++ (Compiler-Explorer-Build-gcc--binutils-2.38) 12.2.0 (see https://godbolt.org/z/E9KhTT4f6)