From: Jim Blandy <jimb@codesourcery.com>
To: msnyder@sonic.net
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH] solib_open, memory leak
Date: Wed, 08 Aug 2007 21:46:00 -0000 [thread overview]
Message-ID: <m34pj9vgvr.fsf@codesourcery.com> (raw)
In-Reply-To: <18682.12.7.175.2.1186597784.squirrel@webmail.sonic.net> (msnyder@sonic.net's message of "Wed, 8 Aug 2007 11:29:44 -0700 (PDT)")
msnyder@sonic.net writes:
>> I think the invariant should be that, when found_file becomes >= 0,
>> then temp_pathname is malloc'd, and not before. The 'openp' clauses
>> will preserve that. So I think you need:
>>
>> if (found_file >= 0)
>> temp_pathname = xstrdup (temp_pathname);
>>
>> after the 'open'.
>>
>> And then there's no need for the xstrdup at the bottom; just return
>> temp_pathname, or xfree it if the caller doesn't want it.
>
> That sounds great, except for one thing (which I had also not addressed).
>
> If openp receives an xmalloc'd temp_pathname, it will clobber it
> without freeing it.
>
>
> I'm thinking that openp will never use the buffer, only the pointer.
Right.
> So rather than what you suggest, what if we AVOID xmalloc before
> openp and just make sure that we pass openp a null pointer or a pointer
> to memory that does not need to be freed (such as alloca).
>
> Umm, thus:
You're right that temp_pathname needs to be NULL if we haven't found
something, so that the *found_pathname code at the end works right.
The code clearly deserves a comment like:
/* We try to find the library in various ways. After each
attempt, either found_file >= 0 and temp_pathname is a malloc'd
string, or found_file < 0 and temp_pathname does not point to
storage that needs to be freed. */
In that light, it seems clearer to me to just put:
if (found_file < 0)
temp_pathname = NULL;
else
temp_pathname = xstrdup (temp_pathname);
immediately after the open, to make it obvious that the rule is
followed there.
The stuff about making in_pathname relative doesn't affect whether
anything is found or the allocatedness of temp_pathname, so it should
be left alone.
> *************** solib_open (char *in_pathname, char **fo
> *** 224,229 ****
> --- 235,247 ----
>
> /* Done. If not found, tough luck. Return found_file and
> (optionally) found_pathname. */
> + if (temp_pathname)
> + {
> + if (found_pathname)
> + *found_pathname = temp_pathname;
> + else
> + xfree (temp_pathname);
> + }
> if (found_pathname != NULL && temp_pathname != NULL)
> *found_pathname = xstrdup (temp_pathname);
> return found_file;
Don't you want those new lines to replace the two that followed them,
not just precede them?
next prev parent reply other threads:[~2007-08-08 21:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-08-05 3:17 msnyder
2007-08-06 22:28 ` Jim Blandy
2007-08-06 22:39 ` Kevin Buettner
2007-08-08 18:29 ` msnyder
2007-08-08 21:46 ` Jim Blandy [this message]
2007-08-08 21:58 ` msnyder
2007-08-08 22:08 ` Jim Blandy
2007-08-09 18:37 ` msnyder
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m34pj9vgvr.fsf@codesourcery.com \
--to=jimb@codesourcery.com \
--cc=gdb-patches@sourceware.org \
--cc=msnyder@sonic.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox