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

            Bug ID: 107264
           Summary: -O1 disables -Wfree-nonheap-object on a map
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: federico at kircheis dot it
  Target Milestone: ---

Consider following code

----
#include <map>

struct Opaque;


void foo1(std::map<int*, Opaque*> v){
    for (auto itr = v.begin(); itr != v.end(); ++itr) {
        delete &itr->second;
    }
}
----

when compiled without parameters, gcc diagnoses "warning: 'void operator
delete(void*, std::size_t)' called on pointer '<unknown>' with nonzero offset 8
[-Wfree-nonheap-object]
delete &itr->second;"

But when compiling at least with -O1, the diagnosis is done, eve when
explicitely adding "-Wfree-nonheap-object" as parameter:
https://godbolt.org/z/Percs3nqa

I've tested different scenarios (see https://godbolt.org/z/eba9axb1h), with a
for-each loop, for example, the diagnosis is active even with -O1.

Reply via email to