This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/56370] New: RFE: warn on 'foo = realloc(foo, â)'
- From: "dwmw2 at infradead dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sun, 17 Feb 2013 22:54:30 +0000
- Subject: [Bug c/56370] New: RFE: warn on 'foo = realloc(foo, â)'
- Auto-submitted: auto-generated
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.