This is sources Bugzilla
Bugzilla Version 2.17.5
Bugzilla Bug 2801
  Bug in gethostbyname_r() example Last modified: 2006-06-19 15:02:09
     Query page      Enter new bug
Bug#: 2801   Hardware:   Reporter: Andy Parkins <andyparkins@gmail.com>
Host: Target: Build:
Product:     Add CC:
Component:   Version:   CC:
Remove selected CCs
Status: NEW   Priority:  
Resolution:   Severity:  
Assigned To: Roland McGrath <roland@gnu.org>   Target Milestone:  
Flags: Requestee:
  backport ()
  examined ()
  testsuite ()
Summary:
Keywords:

Attachment Description Type Created Actions
Create a New Attachment (proposed patch, testcase, etc.) View All

Bug 2801 depends on: Show dependency tree
Show dependency graph
Bug 2801 blocks:

Additional Comments:


Leave as NEW 
Mark bug as waiting for feedback
Mark bug as suspended
Accept bug (change status to ASSIGNED)
Resolve bug, changing resolution to
Resolve bug, mark it as duplicate of bug #
Reassign bug to
Reassign bug to owner of selected component

View Bug Activity   |   Format For Printing


Description:   Last confirmed: 0000-00-00 00:00 Opened: 2006-06-19 14:51
http://www.gnu.org/software/libc/manual/html_node/Host-Names.html

The gethostbyname_r() call accepts a pointer to a struct hostent (&hostbuf in
the example) and a pointer to a pointer to a struct hostent (&hp in the
example).  The result of the lookup is stored in hostbuf itself and hp is set to
point at that buffer on success.

The hp pointer is returned by the example function, but shouldn't be.  The thing
it points to has automatic scope and so won't exist when the function returns.

Either,
 The example function gethostname() should accept a (hostent*) as a parameter,
or
 The hostbuf variable should be allocated statically (although that would
 defeat the purpose of a gethostbyname_r() call in the first place)

There is a secondary problem in that there is no (direct) way that the pointer 
to the auxiliary buffer is passed back to the caller for later release of the 
allocated memory - leading to a memory leak.  Perhaps that could be ignored in
example code though?

------- Additional Comment #1 From Andy Parkins 2006-06-19 15:02 -------
In case it's of use, the following would work (but it's not very pretty):

/* Called like this:
	struct hostent myhost;
	char *auxbuf;
	auxbuf = gethostname( myhost, "www.example.com" );
	if( auxbuf ) {
		/* ... Do something with myhost ... */
		free( auxbuf );
	}
*/
char *gethostname( struct hostent *hostbuf, const char *host)
{
	struct hostent hostbuf, *hp;
	size_t hstbuflen;
	char *tmphstbuf;
	int res;
	int herr;

	hstbuflen = 1024;
	tmphstbuf = malloc (hstbuflen);

	while ((res = gethostbyname_r( host, hostbuf, tmphstbuf, hstbuflen,
					&hp, &herr)) == ERANGE)
	{
		/* Enlarge the buffer.  */
		hstbuflen *= 2;
		tmphstbuf = realloc (tmphstbuf, hstbuflen);
	}
	/*  Check for errors.  */
	if (res || hp == NULL)
		return NULL;
	return tmphstbuf;
}

     Query page      Enter new bug
Actions: New | Query | bug # | Reports | Requests   New Account | Log In