diff mbox series

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

Message ID 20211112142114.239913-4-adhemerval.zanella@linaro.org
State Superseded
Headers show
Series Fixes for getnameinfo() with NI_NOFQDN | expand

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Adhemerval Zanella Nov. 12, 2021, 2:21 p.m. UTC
It aligns NI_NOFQDN with default behavior for getnameinfo().

Checked on x86_64-linux-gnu.
---
 inet/getnameinfo.c | 55 +++++++++++++++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 20 deletions(-)

Comments

Andreas Schwab Nov. 12, 2021, 3:44 p.m. UTC | #1
On Nov 12 2021, Adhemerval Zanella via Libc-alpha wrote:

> diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c
> index 2d2397e7dc..2bec3263fc 100644
> --- a/inet/getnameinfo.c
> +++ b/inet/getnameinfo.c
> @@ -83,7 +83,7 @@ libc_freeres_ptr (static char *domain);
>     now ignored.  */
>  #define DEPRECATED_NI_IDN 192
>  
> -static void
> +static bool
>  nrl_domainname_core (struct scratch_buffer *tmpbuf)
>  {
>    char *c;
> @@ -97,7 +97,7 @@ nrl_domainname_core (struct scratch_buffer *tmpbuf)
>        if (herror == NETDB_INTERNAL && errno == ERANGE)
>  	{
>  	  if (!scratch_buffer_grow (tmpbuf))
> -	    return;
> +	    return false;
>  	}
>        else
>  	break;
> @@ -111,10 +111,13 @@ nrl_domainname_core (struct scratch_buffer *tmpbuf)
>  	 now to get more information.  */
>        while (__gethostname (tmpbuf->data, tmpbuf->length))
>  	if (!scratch_buffer_grow (tmpbuf))
> -	  return;
> +	  return false;
>  
>        if ((c = strchr (tmpbuf->data, '.')) != NULL)
> -	domain = __strdup (++c);
> +	{
> +	  domain = __strdup (++c);
> +	  return domain != NULL;
> +	}
>        else

You can remove `else' here and save a level of indentation.

Andreas.
diff mbox series

Patch

diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c
index 2d2397e7dc..2bec3263fc 100644
--- a/inet/getnameinfo.c
+++ b/inet/getnameinfo.c
@@ -83,7 +83,7 @@  libc_freeres_ptr (static char *domain);
    now ignored.  */
 #define DEPRECATED_NI_IDN 192
 
-static void
+static bool
 nrl_domainname_core (struct scratch_buffer *tmpbuf)
 {
   char *c;
@@ -97,7 +97,7 @@  nrl_domainname_core (struct scratch_buffer *tmpbuf)
       if (herror == NETDB_INTERNAL && errno == ERANGE)
 	{
 	  if (!scratch_buffer_grow (tmpbuf))
-	    return;
+	    return false;
 	}
       else
 	break;
@@ -111,10 +111,13 @@  nrl_domainname_core (struct scratch_buffer *tmpbuf)
 	 now to get more information.  */
       while (__gethostname (tmpbuf->data, tmpbuf->length))
 	if (!scratch_buffer_grow (tmpbuf))
-	  return;
+	  return false;
 
       if ((c = strchr (tmpbuf->data, '.')) != NULL)
-	domain = __strdup (++c);
+	{
+	  domain = __strdup (++c);
+	  return domain != NULL;
+	}
       else
 	{
 	  /* We need to preserve the hostname.  */
@@ -127,14 +130,17 @@  nrl_domainname_core (struct scratch_buffer *tmpbuf)
 	      if (herror == NETDB_INTERNAL && errno == ERANGE)
 		{
 		  if (!scratch_buffer_grow_preserve (tmpbuf))
-		    return;
+		    return false;
 		}
 	      else
 		break;
 	    }
 
 	  if (h != NULL && (c = strchr(h->h_name, '.')) != NULL)
-	    domain = __strdup (++c);
+	    {
+	      domain = __strdup (++c);
+	      return domain != NULL;
+	    }
 	  else
 	    {
 	      struct in_addr in_addr;
@@ -151,20 +157,24 @@  nrl_domainname_core (struct scratch_buffer *tmpbuf)
 		  if (herror == NETDB_INTERNAL && errno == ERANGE)
 		    {
 		      if (!scratch_buffer_grow (tmpbuf))
-			return;
+			return false;
 		    }
 		  else
 		    break;
 		}
 
 	      if (h != NULL && (c = strchr (h->h_name, '.')) != NULL)
-		domain = __strdup (++c);
+		{
+		  domain = __strdup (++c);
+		  return domain != NULL;
+		}
 	    }
 	}
     }
+  return true;
 }
 
-static char *
+static bool
 nrl_domainname (void)
 {
   static int not_first;
@@ -172,6 +182,8 @@  nrl_domainname (void)
   if (__glibc_likely (atomic_load_acquire (&not_first) != 0))
     return domain;
 
+  int r = true;
+
   __libc_lock_define_initialized (static, lock);
   __libc_lock_lock (lock);
 
@@ -180,16 +192,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);
 
       scratch_buffer_free (&tmpbuf);
-
-      atomic_store_release (&not_first, 1);
     }
 
   __libc_lock_unlock (lock);
 
-  return domain;
+  return r;
 };
 
 /* Copy a string to a destination buffer with length checking.  Return
@@ -285,13 +296,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;
+
+	  char *c = domain;
+	  if (c != NULL && (c = strstr (h->h_name, c))
+	       && (c != h->h_name) && (*(--c) == '.'))
+	    /* Terminate the string after the prefix.  */
+	    *c = '\0';
+	}
 
       /* If requested, convert from the IDN format.  */
       bool do_idn = flags & NI_IDN;