unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] nss_dns: Adjust ns_name_ntop calls for gethostby*, getaddrinfo
@ 2019-03-08 20:46 Florian Weimer
  2019-03-12  2:28 ` DJ Delorie
  0 siblings, 1 reply; 2+ messages in thread
From: Florian Weimer @ 2019-03-08 20:46 UTC (permalink / raw
  To: libc-alpha

ns_name_ntop failure always means that the output buffer is not
large enough.

2019-03-08  Florian Weimer  <fweimer@redhat.com>

	* resolv/nss_dns/dns-host.c (getanswer_r, gaih_getanswer_slice):
	On ns_name_ntop failure, the buffer is always too small.

diff --git a/resolv/nss_dns/dns-host.c b/resolv/nss_dns/dns-host.c
index a18b8a6bf4..f04e08016c 100644
--- a/resolv/nss_dns/dns-host.c
+++ b/resolv/nss_dns/dns-host.c
@@ -698,23 +698,18 @@ getanswer_r (struct resolv_context *ctx,
 
   n = __ns_name_unpack (answer->buf, end_of_message, cp,
 			packtmp, sizeof packtmp);
-  if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
+  if (__glibc_unlikely (n < 0))
     {
-      if (__glibc_unlikely (errno == EMSGSIZE))
-	goto too_small;
-
-      n = -1;
+      *errnop = errno;
+      *h_errnop = NO_RECOVERY;
+      return NSS_STATUS_UNAVAIL;
     }
+  if (__ns_name_ntop (packtmp, bp, linebuflen) < 0)
+    goto too_small;
 
-  if (n > 0 && bp[0] == '.')
+  if (bp[0] == '.')
     bp[0] = '\0';
 
-  if (__glibc_unlikely (n < 0))
-    {
-      *errnop = errno;
-      *h_errnop = NO_RECOVERY;
-      return NSS_STATUS_UNAVAIL;
-    }
   if (__glibc_unlikely (name_ok (bp) == 0))
     {
       errno = EBADMSG;
@@ -761,15 +756,16 @@ getanswer_r (struct resolv_context *ctx,
 
       n = __ns_name_unpack (answer->buf, end_of_message, cp,
 			    packtmp, sizeof packtmp);
-      if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
+      if (n < 0)
 	{
-	  if (__glibc_unlikely (errno == EMSGSIZE))
-	    goto too_small;
-
-	  n = -1;
+	  ++had_error;
+	  continue;
 	}
 
-      if (__glibc_unlikely (n < 0 || (*name_ok) (bp) == 0))
+      if (__ns_name_ntop (packtmp, bp, linebuflen) < 0)
+	goto too_small;
+
+      if (__glibc_unlikely ((*name_ok) (bp) == 0))
 	{
 	  ++had_error;
 	  continue;
@@ -892,15 +888,14 @@ getanswer_r (struct resolv_context *ctx,
 
 	  n = __ns_name_unpack (answer->buf, end_of_message, cp,
 				packtmp, sizeof packtmp);
-	  if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
+	  if (n < 0)
 	    {
-	      if (__glibc_unlikely (errno == EMSGSIZE))
-		goto too_small;
-
-	      n = -1;
+	      ++had_error;
+	      break;
 	    }
-
-	  if (__glibc_unlikely (n < 0 || res_hnok (bp) == 0))
+	  if (__ns_name_ntop (packtmp, bp, linebuflen) < 0)
+	    goto too_small;
+	  if (__glibc_unlikely (res_hnok (bp) == 0))
 	    {
 	      ++had_error;
 	      break;
@@ -1032,27 +1027,22 @@ gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname,
   u_char packtmp[NS_MAXCDNAME];
   int n = __ns_name_unpack (answer->buf, end_of_message, cp,
 			    packtmp, sizeof packtmp);
+  if (__glibc_unlikely (n < 0))
+    {
+      *errnop = errno;
+      *h_errnop = NO_RECOVERY;
+      return NSS_STATUS_UNAVAIL;
+    }
   /* We unpack the name to check it for validity.  But we do not need
      it later.  */
-  if (n != -1 && __ns_name_ntop (packtmp, buffer, buflen) == -1)
+  if (__ns_name_ntop (packtmp, buffer, buflen) < 0)
     {
-      if (__glibc_unlikely (errno == EMSGSIZE))
-	{
-	too_small:
-	  *errnop = ERANGE;
-	  *h_errnop = NETDB_INTERNAL;
-	  return NSS_STATUS_TRYAGAIN;
-	}
-
-      n = -1;
+    too_small:
+      *errnop = ERANGE;
+      *h_errnop = NETDB_INTERNAL;
+      return NSS_STATUS_TRYAGAIN;
     }
 
-  if (__glibc_unlikely (n < 0))
-    {
-      *errnop = errno;
-      *h_errnop = NO_RECOVERY;
-      return NSS_STATUS_UNAVAIL;
-    }
   if (__glibc_unlikely (res_hnok (buffer) == 0))
     {
       errno = EBADMSG;
@@ -1078,15 +1068,14 @@ gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname,
     {
       n = __ns_name_unpack (answer->buf, end_of_message, cp,
 			    packtmp, sizeof packtmp);
-      if (n != -1 &&
-	  (h_namelen = __ns_name_ntop (packtmp, buffer, buflen)) == -1)
+      if (n < 0)
 	{
-	  if (__glibc_unlikely (errno == EMSGSIZE))
-	    goto too_small;
-
-	  n = -1;
+	  ++had_error;
+	  continue;
 	}
-      if (__glibc_unlikely (n < 0 || res_hnok (buffer) == 0))
+      if ((h_namelen = __ns_name_ntop (packtmp, buffer, buflen)) < 0)
+	goto too_small;
+      if (__glibc_unlikely (res_hnok (buffer) == 0))
 	{
 	  ++had_error;
 	  continue;

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] nss_dns: Adjust ns_name_ntop calls for gethostby*, getaddrinfo
  2019-03-08 20:46 [PATCH] nss_dns: Adjust ns_name_ntop calls for gethostby*, getaddrinfo Florian Weimer
@ 2019-03-12  2:28 ` DJ Delorie
  0 siblings, 0 replies; 2+ messages in thread
From: DJ Delorie @ 2019-03-12  2:28 UTC (permalink / raw
  To: Florian Weimer; +Cc: libc-alpha

Florian Weimer <fweimer@redhat.com> writes:

> ns_name_ntop failure always means that the output buffer is not
> large enough.
>
> 2019-03-08  Florian Weimer  <fweimer@redhat.com>
>
> 	* resolv/nss_dns/dns-host.c (getanswer_r, gaih_getanswer_slice):
> 	On ns_name_ntop failure, the buffer is always too small.

LGTM

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-03-12  2:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-03-08 20:46 [PATCH] nss_dns: Adjust ns_name_ntop calls for gethostby*, getaddrinfo Florian Weimer
2019-03-12  2:28 ` DJ Delorie

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).