[Bug c++/100876] New: -Wmismatched-new-delete should either look through or ignore placement new

redbeard0531 at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed Jun 2 13:46:00 GMT 2021


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

            Bug ID: 100876
           Summary: -Wmismatched-new-delete should either look through or
                    ignore placement new
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redbeard0531 at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/KTTMrEGns

Example code:
    free(new (malloc(4)) int()); // Warns but shouldn't
    delete new (malloc(4)) int(); // Doesn't warn but should

output:

<source>:5:9: warning: 'void free(void*)' called on pointer returned from a
mismatched allocation function [-Wmismatched-new-delete]
    5 |     free(new (malloc(4)) int()); // Warns but shouldn't
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~
<source>:5:30: note: returned from 'void* operator new(std::size_t, void*)'
    5 |     free(new (malloc(4)) int()); // Warns but shouldn't
      |                              ^

While it would be nice to have a warning on the second line, not warning on the
first seems more important. And hopefully is a backportable fix.

Here is some Real World Code exhibiting this pattern that g++ currently warns
about when compiling:
https://github.com/facebook/hermes/blob/dfef1abd6d20b196e24c591e225a7003e6337a94/unittests/VMRuntime/StringPrimitiveTest.cpp#L221-L235.
There is also an example using calloc() lower in that file.


More information about the Gcc-bugs mailing list