[cfe-dev] Ownership attribute for malloc etc. checking

Andrew McGregor andrewmcgr at gmail.com
Thu Jul 1 20:08:05 PDT 2010


Ok, I get what you're saying, PreVisit seems the right answer.

On Fri, Jul 2, 2010 at 2:41 PM, Jordy Rose <jediknil at belkadan.com> wrote:

>
>
> > struct it *  __attribute((ownership_returns(malloc))) foo(void) {
> >   struct it *rv = malloc(sizeof(struct it));
> >   if (!rv)
> >     return NULL; // Does not warn here.
> >   char *textString = malloc(128*sizeof(char));
> >   if(textString == NULL)
> >     free(rv);
> >     return NULL; // Warns about a memory leak here
> >   rv->s = textString;
> >   return rv; // Does NOT warn here
> > }
>
> ...the code is just missing braces around the second if -- the second
> "return NULL" is unconditional!
>
> Clang should catch this. Filing a bug. *grin*
>

D'oh!

So looking at this version:

void  __attribute((ownership_returns(malloc))) foo2(void) {
  struct it *rv = malloc(sizeof(struct it));
  if (!rv)
    return NULL;
  char *textString = malloc(128*sizeof(char));
  if(textString == NULL) {
    free(rv);
    return NULL;
  }
  rv->s = textString;
  return rv; // warns of a leak here
}

How could I make the assignment before the final return relinquish ownership
of the pointer?

Andrew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100702/5a49b8b1/attachment.html>


More information about the cfe-dev mailing list