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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
We have been discussing this feature for some time now.  Not just the
diagnostics but also the significant optimization opportunities here.

Detecting the simple cases is fairly easy but of only limited value.  The more
interesting cases are harder to handle (and Clang doesn't).  For example, the
bug in the common idiom below isn't diagnosed:

  void f (void*);

  void g (int n)
  {
    char a[8];
    char *p;
    if (n <= 8)
      p = a;
    else
      p = new char[n];

    f (p);

    if (p != a)    
      delete p;
  }

I think marking up related sets of functions (like calloc/malloc/realloc +
free) is necessary for a general solution.  Otherwise it will be limited only
to the subset known/visible to the compiler: i.e., built-ins and inlined
functions.  Beyond function attributes like those suggested in comment #0, it
might be helpful to also provide a similar annotation for pointer variables
(especially members) so that the compiler can make assumptions about the
allocation/deallocation functions expected to be used with them even across
calls to functions without any annotation.

Reply via email to