gethostbyname_r problem in glibc-2.2.93

Glen Nakamura glen@imodulo.com
Wed Sep 11 18:23:00 GMT 2002


Aloha,

The following change causes the return value of gethostbyname_r to
disagree with the glibc documentation:

2002-08-25  Ulrich Drepper  <drepper@redhat.com>

	* nss/getXXbyYY_r.c (REENTRANT_NAME): Return ENOENT if status is
	neither SUCCESS nor TRYAGAIN [PR libc/4259].

===================================================================
RCS file: /cvs/glibc/libc/nss/getXXbyYY_r.c,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -r1.42 -r1.43
--- libc/nss/getXXbyYY_r.c	2002/08/03 06:25:51	1.42
+++ libc/nss/getXXbyYY_r.c	2002/08/26 06:15:30	1.43
@@ -229,7 +229,8 @@
 #ifdef POSTPROCESS
   POSTPROCESS;
 #endif
-  return status == NSS_STATUS_SUCCESS ? 0 : errno;
+  return (status == NSS_STATUS_SUCCESS
+	  ? 0 : (status == NSS_STATUS_TRYAGAIN ? EAGAIN : ENOENT));
 }
 
 
According to the documentation:

     If the function [gethostbyname_r] failed the return value is an error
     number.  In addition to the errors defined for `gethostbyname' it
     can also be `ERANGE'.  In this case the call should be repeated
     with a larger buffer.

Currently, gethostbyname_r returns EAGAIN instead of ERANGE and breaks
compatibility w/ glibc-2.2.5.  I've attached a slightly modified version of
the example code from the documentation which fails because ERANGE
is not returned (tested on alpha-pc-linux-gnu and i586-pc-linux-gnu).

- glen

-------------- next part --------------
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>

struct hostent *
gethostname (char *host)
{
  struct hostent hostbuf, *hp;
  size_t hstbuflen;
  char *tmphstbuf;
  int res;
  int herr;

  hstbuflen = 256;
  /* Allocate buffer, remember to free it to avoid memory leakage.  */
  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 hp;
}

int
main ()
{
  if (gethostname ("sources.redhat.com") == NULL)
    abort ();
  return 0;
}


More information about the Libc-alpha mailing list