This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PING][PATCH][BZ #2801] Fix gethostbyname_r example.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Andreas Schwab <schwab at linux-m68k dot org>
- Cc: libc-alpha at sourceware dot org
- Date: Mon, 21 Oct 2013 09:01:49 +0200
- Subject: [PING][PATCH][BZ #2801] Fix gethostbyname_r example.
- Authentication-results: sourceware.org; auth=none
- References: <20131016162118 dot GA17493 at domone dot podge> <8738o1uq4q dot fsf at igel dot home> <20131016173112 dot GA18408 at domone dot podge>
On Wed, Oct 16, 2013 at 07:31:12PM +0200, OndÅej BÃlka wrote:
> On Wed, Oct 16, 2013 at 06:39:17PM +0200, Andreas Schwab wrote:
> > How about fixing the memory leak as well?
> >
> here.
>
> [BZ #2801]
> * manual/socket.texi: Fix gethostbyname_r example.
>
> diff --git a/manual/socket.texi b/manual/socket.texi
> index 25c35c4..4c7e623 100644
> --- a/manual/socket.texi
> +++ b/manual/socket.texi
> @@ -1307,23 +1307,25 @@ Here's a small example:
> struct hostent *
> gethostname (char *host)
> @{
> - struct hostent hostbuf, *hp;
> + struct hostent *hostbuf, *hp;
> size_t hstbuflen;
> char *tmphstbuf;
> int res;
> int herr;
>
> + hostbuf = malloc (sizeof (struct hostent));
> hstbuflen = 1024;
> - /* Allocate buffer, remember to free it to avoid memory leakage. */
> tmphstbuf = malloc (hstbuflen);
>
> - while ((res = gethostbyname_r (host, &hostbuf, tmphstbuf, hstbuflen,
> + while ((res = gethostbyname_r (host, hostbuf, tmphstbuf, hstbuflen,
> &hp, &herr)) == ERANGE)
> @{
> /* Enlarge the buffer. */
> hstbuflen *= 2;
> tmphstbuf = realloc (tmphstbuf, hstbuflen);
> @}
> +
> + free (tmphstbuf);
> /* Check for errors. */
> if (res || hp == NULL)
> return NULL;
--
Operators killed when huge stack of backup tapes fell over.