This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [RFC] replace malloc with a decl on the stack
- From: Florian Weimer <fweimer at redhat dot com>
- To: Marc Glisse <marc dot glisse at inria dot fr>, Ondřej Bílka <neleai at seznam dot cz>
- Cc: Richard Biener <richard dot guenther at gmail dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Sun, 24 Nov 2013 14:22:32 +0100
- Subject: Re: [RFC] replace malloc with a decl on the stack
- Authentication-results: sourceware.org; auth=none
- References: <alpine dot DEB dot 2 dot 10 dot 1311100140300 dot 4153 at laptop-mg dot saclay dot inria dot fr> <CAFiYyc0B8OurmAh=h4uEFQcbsTTrHNgYEvgrKxTpsOOce52D7A at mail dot gmail dot com> <alpine dot DEB dot 2 dot 10 dot 1311120001180 dot 17040 at laptop-mg dot saclay dot inria dot fr> <20131112150723 dot GA26108 at domone dot podge> <alpine dot DEB dot 2 dot 10 dot 1311121613240 dot 23017 at stedding dot saclay dot inria dot fr>
On 11/12/2013 04:22 PM, Marc Glisse wrote:
On Tue, 12 Nov 2013, Ondřej Bílka wrote:
Anyway you need a better analysis to determine if user called realloc on
converted pointer.
Note that I am checking if the argument of free is the same as the
return value of malloc by looking at the SSA_NAME, not the C variable.
If the user called realloc on this value and then calls free on the same
value on all paths (including those where realloc returned a different
value), that's a broken program to start with. But indeed my current
patch fails on:
p=malloc(12);
q=realloc(p,16);
if(p==q)free(p);
exit(0);
because I (wrongly apparently) considered exit as safe as free :-(
Strictly speaking, this code is equivalent to:
p=malloc(12);
q=realloc(p,16);
free(p);
exit(0);
So I don't see how your optimization breaks it if it is applied to it. :)
p==q is undefined if the realloc call returned a different pointer.
Past-realloc pointer uses are fairly common, though, and often used to
adjust interior pointers. Empirically, this seem to work, although I
wouldn't be surprised if we already have optimizations which make
past-realloc pointer use undefined.
(I don't think C provides a safe way to update interior pointers in
place, FWIW.)
--
Florian Weimer / Red Hat Product Security Team