This is split from Bug 87731, as recommended by Jonathan Wakely: Valgrind 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*);
Note that you can have a custom malloc function (e.g. xmalloc) that pairs with standard free, so it should be possible to indicate that.
Something like this is probably necessary for a robust implementation of pr87731. It won't do much in GCC on its own, though it could be used by third party static analyzers (that suggests that we might want to consider adopting one or more existing forms of annotations already recognized by such tools). This is also a general mechanism, not one specific to C++, so changing Component to middle-end.
The solution I'm working on for pr91582 adds tracking of dynamically allocated objects to the strlen pass (including alloca, VLAs, and user-defined allocation functions declared with attribute alloc_size). A natural extension of the project is to detect accesses to deallocated objects. A new function attribute such as "free" to parallel attributes malloc and alloca_size will be required to detect such deallocation by user-defined functions (the attribute will need to apply to specific function arguments to make it possible to use it with functions taking two more pointers). I expect to submit my patch for pr91582 in time for GCC 10 but the new attribute will most likely have to wait until GCC 11.
See also pr94527 for a related request. I'm working on a solution for GCC 11 (fingers crossed).
Implemented GCC 11 in r11-5732.