public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org
Cc: leonardo.macchia@gmail.com
Subject: [PATCH 3/3] inet: Return EAI_MEMORY when nrl_domainname() fails to allocate memory
Date: Wed, 10 Nov 2021 15:58:32 -0300	[thread overview]
Message-ID: <20211110185832.1931688-4-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20211110185832.1931688-1-adhemerval.zanella@linaro.org>

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 --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;
-- 
2.32.0


      parent reply	other threads:[~2021-11-10 18:58 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-10 18:58 [PATCH 0/3] Fixes for getnameinfo() with NI_NOFQDN Adhemerval Zanella
2021-11-10 18:58 ` [PATCH 1/3] inet: Fix getnameinfo (NI_NOFQDN) race condition (BZ#28566) Adhemerval Zanella
2021-11-11  8:16   ` Florian Weimer
2021-11-11 13:34     ` Adhemerval Zanella
2021-11-11 13:54       ` Florian Weimer
2021-11-11 14:09         ` Adhemerval Zanella
2021-11-11 14:12           ` Florian Weimer
2021-11-11 14:17             ` Adhemerval Zanella
2021-11-11 17:41               ` Florian Weimer
2021-11-10 18:58 ` [PATCH 2/3] inet: Remove strdupa from nrl_domainname() Adhemerval Zanella
2021-11-11  8:18   ` Florian Weimer
2021-11-11 13:36     ` Adhemerval Zanella
2021-11-11 14:00       ` Florian Weimer
2021-11-10 18:58 ` Adhemerval Zanella [this message]

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=20211110185832.1931688-4-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=leonardo.macchia@gmail.com \
    --cc=libc-alpha@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).