public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] getaddrinfo: Get rid of alloca
@ 2023-06-30 14:30 Joe Simmons-Talbott
  2023-08-29 14:56 ` Joe Simmons-Talbott
  2023-08-29 15:09 ` Andreas Schwab
  0 siblings, 2 replies; 3+ messages in thread
From: Joe Simmons-Talbott @ 2023-06-30 14:30 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joe Simmons-Talbott

Use a scratch_buffer rather than alloca to avoid potential stack
overflow.
---
Changes to v1:
  * Don't use alloc_size but rather rely on * scratch_buffer_set_array_size's
    overflow checks.

 sysdeps/posix/getaddrinfo.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)

diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
index 0356b622be..be3b3f6e45 100644
--- a/sysdeps/posix/getaddrinfo.c
+++ b/sysdeps/posix/getaddrinfo.c
@@ -2404,22 +2404,17 @@ getaddrinfo (const char *name, const char *service,
       struct addrinfo *q;
       struct addrinfo *last = NULL;
       char *canonname = NULL;
-      bool malloc_results;
-      size_t alloc_size = nresults * (sizeof (*results) + sizeof (size_t));
+      struct scratch_buffer buf;
+      scratch_buffer_init (&buf);
 
-      malloc_results
-	= !__libc_use_alloca (alloc_size);
-      if (malloc_results)
+      if (!scratch_buffer_set_array_size (&buf, nresults,
+					  sizeof (*results) + sizeof (size_t)))
 	{
-	  results = malloc (alloc_size);
-	  if (results == NULL)
-	    {
-	      __free_in6ai (in6ai);
-	      return EAI_MEMORY;
-	    }
+	  __free_in6ai (in6ai);
+	  return EAI_MEMORY;
 	}
-      else
-	results = alloca (alloc_size);
+      results = buf.data;
+
       order = (size_t *) (results + nresults);
 
       /* Now we definitely need the interface information.  */
@@ -2590,8 +2585,7 @@ getaddrinfo (const char *name, const char *service,
       /* Fill in the canonical name into the new first entry.  */
       p->ai_canonname = canonname;
 
-      if (malloc_results)
-	free (results);
+      scratch_buffer_free (&buf);
     }
 
   __free_in6ai (in6ai);
-- 
2.39.2


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

* Re: [PATCH v2] getaddrinfo: Get rid of alloca
  2023-06-30 14:30 [PATCH v2] getaddrinfo: Get rid of alloca Joe Simmons-Talbott
@ 2023-08-29 14:56 ` Joe Simmons-Talbott
  2023-08-29 15:09 ` Andreas Schwab
  1 sibling, 0 replies; 3+ messages in thread
From: Joe Simmons-Talbott @ 2023-08-29 14:56 UTC (permalink / raw)
  To: libc-alpha

Ping.

On Fri, Jun 30, 2023 at 10:30:50AM -0400, Joe Simmons-Talbott wrote:
> Use a scratch_buffer rather than alloca to avoid potential stack
> overflow.
> ---
> Changes to v1:
>   * Don't use alloc_size but rather rely on * scratch_buffer_set_array_size's
>     overflow checks.
> 
>  sysdeps/posix/getaddrinfo.c | 24 +++++++++---------------
>  1 file changed, 9 insertions(+), 15 deletions(-)
> 
> diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
> index 0356b622be..be3b3f6e45 100644
> --- a/sysdeps/posix/getaddrinfo.c
> +++ b/sysdeps/posix/getaddrinfo.c
> @@ -2404,22 +2404,17 @@ getaddrinfo (const char *name, const char *service,
>        struct addrinfo *q;
>        struct addrinfo *last = NULL;
>        char *canonname = NULL;
> -      bool malloc_results;
> -      size_t alloc_size = nresults * (sizeof (*results) + sizeof (size_t));
> +      struct scratch_buffer buf;
> +      scratch_buffer_init (&buf);
>  
> -      malloc_results
> -	= !__libc_use_alloca (alloc_size);
> -      if (malloc_results)
> +      if (!scratch_buffer_set_array_size (&buf, nresults,
> +					  sizeof (*results) + sizeof (size_t)))
>  	{
> -	  results = malloc (alloc_size);
> -	  if (results == NULL)
> -	    {
> -	      __free_in6ai (in6ai);
> -	      return EAI_MEMORY;
> -	    }
> +	  __free_in6ai (in6ai);
> +	  return EAI_MEMORY;
>  	}
> -      else
> -	results = alloca (alloc_size);
> +      results = buf.data;
> +
>        order = (size_t *) (results + nresults);
>  
>        /* Now we definitely need the interface information.  */
> @@ -2590,8 +2585,7 @@ getaddrinfo (const char *name, const char *service,
>        /* Fill in the canonical name into the new first entry.  */
>        p->ai_canonname = canonname;
>  
> -      if (malloc_results)
> -	free (results);
> +      scratch_buffer_free (&buf);
>      }
>  
>    __free_in6ai (in6ai);
> -- 
> 2.39.2
> 


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

* Re: [PATCH v2] getaddrinfo: Get rid of alloca
  2023-06-30 14:30 [PATCH v2] getaddrinfo: Get rid of alloca Joe Simmons-Talbott
  2023-08-29 14:56 ` Joe Simmons-Talbott
@ 2023-08-29 15:09 ` Andreas Schwab
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Schwab @ 2023-08-29 15:09 UTC (permalink / raw)
  To: Joe Simmons-Talbott via Libc-alpha; +Cc: Joe Simmons-Talbott

On Jun 30 2023, Joe Simmons-Talbott via Libc-alpha wrote:

> Use a scratch_buffer rather than alloca to avoid potential stack
> overflow.

Ok.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

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

end of thread, other threads:[~2023-08-29 15:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-30 14:30 [PATCH v2] getaddrinfo: Get rid of alloca Joe Simmons-Talbott
2023-08-29 14:56 ` Joe Simmons-Talbott
2023-08-29 15:09 ` Andreas Schwab

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