https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99714
Bug ID: 99714 Summary: warn about alloca/dealloc mismatches based on calls with same object in different functions Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- Deallocating an object in one function through a pointer that's assigned the result of a mismatched allocation in another function is almost certainly a bug. Diagnosing such mismatched calls would help detect at compile time problems like pr99687 that are currently only detected by dynamic analysis tools. #if __cplusplus struct A { int *p; A (int); ~A (); }; A::A (int n): p (new int[n]{ }) { } A::~A () { delete p; // missing -Wmismatched-new-delete } #else struct A { int *p; }; void dealloc (void*); __attribute__ ((malloc (dealloc))) void* alloc (int); void init (struct A *p, int n) { p = alloc (n * sizeof *p); } void fini (struct A *p) { __builtin_free (p); // missing -Wmismatched-dealloc } #endif