[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

incorrect return value for gethostbyname_r

From: R Sreelatha
Subject: incorrect return value for gethostbyname_r
Date: Tue, 12 Feb 2002 19:16:30 +0530

        I find that the return value of gethostbyname_r is set correctly.
Steps to reproduce:
1. Write a testcase using gethostbyname_r().
2. Execute the testcase with non-existing IP Address (For ex,
Actual results: Return value is zero.
Expected results: Return value should be non-zero, as the function fails
with HOST_NOT_FOUND error in *h_errnop.

By debugging using gdb, I found that at line 226 of file /nss/getXXbyYY_r.c
in glibc-2.2.4:
 "return status == NSS_STATUS_SUCCESS ? 0 : errno;"
the status is not set to NSS_STATUS_SUCCESS, but the errno is 0. Hence
gethostbyname_r  returns 0.

Since we are passing an invalid ipaddress, I would expect
gethostbyname_r to return a non-zero value as given in man pages. Is this a
bug in glibc?

Here is a small testcase to illustrate this.
struct hostent * getname(char *host);
int main(int argc, char* argv[]) {
char *phost;
    printf("Use:  %s <host>\n",argv[0]);
  getname (phost);
struct hostent * getname (char *host)
  struct hostent hostbuf, *hp;
  size_t hstbuflen;
  char *tmphstbuf;
  int res;
  int herr;

  hstbuflen = 1024;

  tmphstbuf = (char *) malloc (hstbuflen);

  while ((res = gethostbyname_r (host, &hostbuf, tmphstbuf, hstbuflen,
                                 &hp, &herr)) == ERANGE)
      /* Enlarge the buffer.  */
      hstbuflen *= 2;
      tmphstbuf = (char *) realloc (tmphstbuf, hstbuflen);
  /*  Check for errors.  */
  printf("The return value is %d\n",res);

  if (res || hp == NULL)
    printf("gethostbyname error for host: %d : %s
    return NULL;
  return hp;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]