This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/56370] New: RFE: warn on 'foo = realloc(foo, â)'


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56370

             Bug #: 56370
           Summary: RFE: warn on 'foo = realloc(foo, â)'
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: dwmw2@infradead.org


I thought I actually knew C, but it's just been pointed out to me that I have a
habit of doing 'foo = realloc(foo, â)', which causes a memory leak in the
failure case because whatever 'foo' used to point to can never be freed.
Looking around, I see the same bug is fairly common.

It's very rarely sane to assign the return value of realloc() directly to the
same variable which was its first argument. Only when you don't *care* about a
memory leak, or where you have another copy of the pointer that used to be in
'foo' and can still free it.

It would be nice if the compiler would warn about this idiom.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]