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

            Bug ID: 87731
           Summary: Detection of mismatched alloc/free pairs
           Product: gcc
           Version: 8.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzi...@poradnik-webmastera.com
  Target Milestone: ---

Following code compiles cleanly on gcc:

void foo()
{
    char* c = new char[4];
    delete c;
}

When it is compiles using clang 7.0.0, it generates following warning. Please
do the same in gcc.

<source>:4:5: warning: 'delete' applied to a pointer that was allocated with
'new[]'; did you mean 'delete[]'? [-Wmismatched-new-delete]
    delete c;
    ^
          []
<source>:3:15: note: allocated with 'new[]' here
    char* c = new char[4];
              ^
1 warning generated.
Compiler returned: 0

Valgrind also has similar diagnostics, it checks checks following pairs by
default: malloc/free, new/delete, new[]/delete[]. Please implement something
similar in gcc.

Valgrind also provides set of macros which allows it to track custom alloc/free
functions. It would be nice if you add new attributes which could be attached
to custom alloc and free functions, so gcc could check pairing for them too. I
think of something like this:

__attribute__((malloc("MyAllocType")))
void* MyAlloc(size_t);

__attribute__((free("MyAllocType")))
void MyFree(void*);

Reply via email to