diff mbox series

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

Message ID 20211110185832.1931688-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. 10, 2021, 6:58 p.m. UTC
It aligns NI_NOFQDN with default behavior for getnameinfo().

Checked on x86_64-linux-gnu.
---
 inet/getnameinfo.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c
index 69a94604bd..16d461428d 100644
--- a/inet/getnameinfo.c
+++ b/inet/getnameinfo.c
@@ -83,9 +83,11 @@  libc_freeres_ptr (static char *domain);
    now ignored.  */
 #define DEPRECATED_NI_IDN 192
 
-static char *
+static bool
 nrl_domainname (void)
 {
+  int r;
+
   __libc_lock_define_initialized (static, lock);
   __libc_lock_lock (lock);
 
@@ -171,15 +173,17 @@  nrl_domainname (void)
 		}
 	    }
 	}
+      not_first = true;
 
     done:
       scratch_buffer_free (&tmpbuf);
-      not_first = true;
     }
 
+  r = not_first;
+
   __libc_lock_unlock (lock);
 
-  return domain;
+  return r;
 };
 
 /* Copy a string to a destination buffer with length checking.  Return
@@ -275,13 +279,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;