[PATCH v2 3/3] inet: Return EAI_MEMORY when nrl_domainname() fails to allocate memory

DJ Delorie dj@redhat.com
Tue Mar 8 04:12:26 GMT 2022


Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org> writes:
> -static void
> +static bool

Ok.  Please include comment saying what true/false return means, though.

>  nrl_domainname_core (struct scratch_buffer *tmpbuf)
>  {
>    char *c;
>    struct hostent *h, th;
>    int herror;
>  
> -  while (__gethostbyname_r ("localhost", &th,
> -			    tmpbuf->data, tmpbuf->length,
> +  while (__gethostbyname_r ("localhost", &th, tmpbuf->data, tmpbuf->length,

Formatting; ok

>  	  if (!scratch_buffer_grow (tmpbuf))
> -	    return;
> +	    return false;

Ok.

>        else
>  	break;
>      }
>  
>    if (h != NULL && (c = strchr (h->h_name, '.')) != NULL)
> -    domain = __strdup (++c);
> -  else
>      {
> +      domain = __strdup (++c);
> +      return domain != NULL;
> +    }

Ok.

> -      /* The name contains no domain information.  Use the name
> -	 now to get more information.  */
> -      while (__gethostname (tmpbuf->data, tmpbuf->length))
> -	if (!scratch_buffer_grow (tmpbuf))
> -	  return;
> +  /* The name contains no domain information.  Use the name
> +     now to get more information.  */
> +  while (__gethostname (tmpbuf->data, tmpbuf->length))
> +    if (!scratch_buffer_grow (tmpbuf))
> +      return false;

Ok.

> -
> -      if ((c = strchr (tmpbuf->data, '.')) != NULL)
> -	domain = __strdup (++c);
> +  if ((c = strchr (tmpbuf->data, '.')) != NULL)
> +    {
> +      domain = __strdup (++c);
> +      return domain != NULL;
> +    }

Ok.

> -      else
> -	{
> -	  /* We need to preserve the hostname.  */
> -	  size_t hstnamelen = strlen (tmpbuf->data) + 1;
> -	  while (__gethostbyname_r (tmpbuf->data, &th,
> -				    tmpbuf->data + hstnamelen,
> -				    tmpbuf->length - hstnamelen,
> -				    &h, &herror))
> -	    {
> -	      if (herror == NETDB_INTERNAL && errno == ERANGE)
> -		{
> -		  if (!scratch_buffer_grow_preserve (tmpbuf))
> -		    return;
> -		}
> -	      else
> -		break;
> -	    }
>  
> +  /* We need to preserve the hostname.  */
> +  size_t hstnamelen = strlen (tmpbuf->data) + 1;
> +  while (__gethostbyname_r (tmpbuf->data, &th, tmpbuf->data + hstnamelen,
> +			    tmpbuf->length - hstnamelen, &h, &herror))
> +    {
> +      if (herror == NETDB_INTERNAL && errno == ERANGE)
> +	{
> +	  if (!scratch_buffer_grow_preserve (tmpbuf))
> +	    return false;
> +	}
> +      else
> +	break;
> +    }

Ok.

> -	  if (h != NULL && (c = strchr(h->h_name, '.')) != NULL)
> -	    domain = __strdup (++c);
> +  if (h != NULL && (c = strchr(h->h_name, '.')) != NULL)
> +    {
> +      domain = __strdup (++c);
> +      return domain != NULL;
> +    }

Ok.

> -	  else
> -	    {
> -	      struct in_addr in_addr;
>  
> -	      in_addr.s_addr = htonl (INADDR_LOOPBACK);
>  
> -	      while (__gethostbyaddr_r ((const char *) &in_addr,
> -					sizeof (struct in_addr),
> -					AF_INET, &th,
> -					tmpbuf->data,
> -					tmpbuf->length,
> -					&h, &herror))
> -		{
> -		  if (herror == NETDB_INTERNAL && errno == ERANGE)
> -		    {
> -		      if (!scratch_buffer_grow (tmpbuf))
> -			return;
> -		    }
> -		  else
> -		    break;
> -		}
>  
> -	      if (h != NULL && (c = strchr (h->h_name, '.')) != NULL)
> -		domain = __strdup (++c);
> -	    }
> +
> +  struct in_addr in_addr = { .s_addr = htonl (INADDR_LOOPBACK) };
> +
> +  while (__gethostbyaddr_r ((const char *) &in_addr, sizeof (struct in_addr),
> +			    AF_INET, &th, tmpbuf->data, tmpbuf->length, &h,
> +			    &herror))
> +    {
> +      if (herror == NETDB_INTERNAL && errno == ERANGE)
> +	{
> +	  if (!scratch_buffer_grow (tmpbuf))
> +	    return false;
>  	}
> +      else
> +	break;
> +    }
> +
> +  if (h != NULL && (c = strchr (h->h_name, '.')) != NULL)
> +    {
> +      domain = __strdup (++c);
> +      return domain != NULL;
>      }
> +  return true;
>  }

Ok.

> -static char *
> +static bool
>  nrl_domainname (void)
>  {
>    static int not_first;
>  
>    if (__glibc_likely (atomic_load_acquire (&not_first) != 0))
> -    return domain;
> +    return true;

If we successfully ran before, shortcut to success again.

> +
> +  int r = true;

Assume we'll succeed again, if more than one thread gets here.

>    __libc_lock_define_initialized (static, lock);
>    __libc_lock_lock (lock);
> @@ -180,16 +181,15 @@ nrl_domainname (void)
>        struct scratch_buffer tmpbuf;
>        scratch_buffer_init (&tmpbuf);
>  
> -      nrl_domainname_core (&tmpbuf);
> +      if ((r = nrl_domainname_core (&tmpbuf)))
> +	atomic_store_release (&not_first, 1);

If the call is successful, we set not_first to enable the above
shortcut, and set r, which we return.

I note that a failing first time through will not set not_first, so
other threads contending for this lock will also call this block, and
continue calling this block, until they succeed.  Therefor the above
shortcut's assumption is valid.

Ok.

>        scratch_buffer_free (&tmpbuf);
> -
> -      atomic_store_release (&not_first, 1);
>      }
>  
>    __libc_lock_unlock (lock);
>  
> -  return domain;
> +  return r;

This will be TRUE if we skip the block because a previous thread
succeeded, or FALSE if we ran the block and failed.

>  };

Ok.

>  /* Copy a string to a destination buffer with length checking.  Return
> @@ -285,13 +285,17 @@ gni_host_inet_name (struct scratch_buffer *tmpbuf,
>  
>    if (h)
>      {
> -      char *c;
> -      if ((flags & NI_NOFQDN)
> -	  && (c = nrl_domainname ())
> -	  && (c = strstr (h->h_name, c))
> -	  && (c != h->h_name) && (*(--c) == '.'))
> -	/* Terminate the string after the prefix.  */
> -	*c = '\0';
> +      if (flags & NI_NOFQDN)
> +	{
> +	  if (!nrl_domainname ())
> +	    return EAI_MEMORY;

Same as first line in above logic; ok.

> +	  char *c = domain;
> +	  if (c != NULL && (c = strstr (h->h_name, c))
> +	       && (c != h->h_name) && (*(--c) == '.'))

Matches remainder of the logic.  Ok.

> +	    /* Terminate the string after the prefix.  */
> +	    *c = '\0';
> +	}

Ok.

LGTM with that one comment added.

Reviewed-by: DJ Delorie <dj@redhat.com>



More information about the Libc-alpha mailing list