getaddrinfo: Always allocate canonical name on the heap

Message ID 7ba94b4d-c575-e122-17d0-8f00edfa21a6@redhat.com
State Committed
Headers

Commit Message

Florian Weimer June 2, 2017, 2:37 p.m. UTC
  On 06/02/2017 04:10 PM, Adhemerval Zanella wrote:
> I noticed on x86_64 (gcc 5.4), powerpc64le (gcc 5.4), aarch64 (gcc 4.9.2),
> and sparc64 (gcc 6.3.1).  My guess is newer GCC versions are inline it.

Fair enough.  What about the attached patch?  I spotted one missing heap
allocation (which is not visible as a bug because the code doesn't try
to pass the pointer to free), and included that as well.

Thanks,
Florian
  

Comments

Adhemerval Zanella Netto June 2, 2017, 5:50 p.m. UTC | #1
On 02/06/2017 11:37, Florian Weimer wrote:
> On 06/02/2017 04:10 PM, Adhemerval Zanella wrote:
>> I noticed on x86_64 (gcc 5.4), powerpc64le (gcc 5.4), aarch64 (gcc 4.9.2),
>> and sparc64 (gcc 6.3.1).  My guess is newer GCC versions are inline it.
> 
> Fair enough.  What about the attached patch?  I spotted one missing heap
> allocation (which is not visible as a bug because the code doesn't try
> to pass the pointer to free), and included that as well.
> 
> Thanks,
> Florian
> 

LGTM, thanks.
  
Joseph Myers June 2, 2017, 9:26 p.m. UTC | #2
I'm still seeing a localplt failure for strdup even after commit 
6257fcfd58479f6b7ae0fdde045b9ff144d543da.

https://sourceware.org/ml/libc-testresults/2017-q2/msg00276.html

(The strdup@plt reference is from gaih_inet.constprop.6, at least on 
x86_64.  It seems there is one plain strdup reference in 
sysdeps/posix/getaddrinfo.c.)
  

Patch

getaddrinfo: Fix localplt failure involving strdup

2017-06-02  Florian Weimer  <fweimer@redhat.com>

	* sysdeps/posix/getaddrinfo.c (gethosts): Eliminate another
	strdupa.
	(getcanonname): Use __strdup instead of strdup.

diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
index d92db70..a8b5bb5 100644
--- a/sysdeps/posix/getaddrinfo.c
+++ b/sysdeps/posix/getaddrinfo.c
@@ -286,9 +286,16 @@  convert_hostent_to_gaih_addrtuple (const struct addrinfo *req,
 	}								      \
       *pat = addrmem;							      \
 									      \
-      if (localcanon !=	NULL && canon == NULL)				      \
-	canon = strdupa (localcanon);					      \
-									      \
+      if (localcanon != NULL && canon == NULL)				      \
+	{								      \
+	  canonbuf = __strdup (localcanon);				      \
+	  if (canonbuf == NULL)						      \
+	    {								      \
+	      result = -EAI_SYSTEM;					      \
+	      goto free_and_return;					      \
+	    }								      \
+	  canon = canonbuf;						      \
+	}								      \
       if (_family == AF_INET6 && *pat != NULL)				      \
 	got_ipv6 = true;						      \
     }									      \
@@ -330,7 +337,7 @@  getcanonname (service_user *nip, struct gaih_addrtuple *at, const char *name)
 	   string.  */
 	s = (char *) name;
     }
-  return strdup (name);
+  return __strdup (name);
 }
 
 static int