public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Adhemerval Zanella <azanella@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] inet: Return EAI_MEMORY when nrl_domainname() fails to allocate memory
Date: Tue,  8 Mar 2022 15:54:14 +0000 (GMT)	[thread overview]
Message-ID: <20220308155414.350E5385C412@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=2da6e439164c54bac4d5fd1320e32f8e16c1a6be

commit 2da6e439164c54bac4d5fd1320e32f8e16c1a6be
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Nov 10 15:32:42 2021 -0300

    inet: Return EAI_MEMORY when nrl_domainname() fails to allocate memory
    
    It aligns NI_NOFQDN with default behavior for getnameinfo().
    
    Checked on x86_64-linux-gnu.

Diff:
---
 inet/getnameinfo.c | 142 ++++++++++++++++++++++++++++-------------------------
 1 file changed, 74 insertions(+), 68 deletions(-)

diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c
index d3387dd16a..4733be6f5b 100644
--- a/inet/getnameinfo.c
+++ b/inet/getnameinfo.c
@@ -83,94 +83,97 @@ libc_freeres_ptr (static char *domain);
    now ignored.  */
 #define DEPRECATED_NI_IDN 192
 
-static void
+/* Return true if no memory allocation failure happened (even if domain
+   name could not be obtained) or false otherwise.  */
+static bool
 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,
 			    &h, &herror))
     {
       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);
-  else
     {
-      /* 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;
-
-      if ((c = strchr (tmpbuf->data, '.')) != NULL)
-	domain = __strdup (++c);
-      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;
-	    }
+      domain = __strdup (++c);
+      return domain != NULL;
+    }
 
-	  if (h != NULL && (c = strchr(h->h_name, '.')) != NULL)
-	    domain = __strdup (++c);
-	  else
-	    {
-	      struct in_addr in_addr;
+  /* 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;
 
-	      in_addr.s_addr = htonl (INADDR_LOOPBACK);
+  if ((c = strchr (tmpbuf->data, '.')) != NULL)
+    {
+      domain = __strdup (++c);
+      return domain != NULL;
+    }
 
-	      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;
-		}
+  /* 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;
+    }
 
-	      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;
+    }
+
+  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;
 }
 
-static char *
+static bool
 nrl_domainname (void)
 {
   static int not_first;
 
   if (__glibc_likely (atomic_load_acquire (&not_first) != 0))
-    return domain;
+    return true;
+
+  int r = true;
 
   __libc_lock_define_initialized (static, lock);
   __libc_lock_lock (lock);
@@ -180,16 +183,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 +287,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;


                 reply	other threads:[~2022-03-08 15:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220308155414.350E5385C412@sourceware.org \
    --to=azanella@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).