public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Joe Simmons-Talbott <josimmon@redhat.com>, libc-alpha@sourceware.org
Subject: Re: [PATCH] setenv.c: Get rid of alloca.
Date: Thu, 29 Jun 2023 11:39:08 -0300	[thread overview]
Message-ID: <afe9f92d-8fdc-7f61-9ea2-5b5403c573f7@linaro.org> (raw)
In-Reply-To: <20230623152517.3268336-1-josimmon@redhat.com>



On 23/06/23 12:25, Joe Simmons-Talbott via Libc-alpha wrote:
> Use a scratch_buffer rather than alloca to avoid potential stack
> overflow.
> ---
>  stdlib/setenv.c | 35 ++++++++++-------------------------
>  1 file changed, 10 insertions(+), 25 deletions(-)
> 
> diff --git a/stdlib/setenv.c b/stdlib/setenv.c
> index ba5257d3bf..90bc30b219 100644
> --- a/stdlib/setenv.c
> +++ b/stdlib/setenv.c
> @@ -44,6 +44,8 @@ extern int errno;
>  # include <unistd.h>
>  #endif
>  
> +#include <scratch_buffer.h>
> +

There is no use of scratch_buffer, the rest looks ok.

>  #if !_LIBC
>  # define __environ	environ
>  # ifndef HAVE_ENVIRON_DECL
> @@ -182,19 +184,14 @@ __add_to_environ (const char *name, const char *value, const char *combined,
>  	{
>  	  const size_t varlen = namelen + 1 + vallen;
>  #ifdef USE_TSEARCH
> -	  char *new_value;
> -	  int use_alloca = __libc_use_alloca (varlen);
> -	  if (__builtin_expect (use_alloca, 1))
> -	    new_value = (char *) alloca (varlen);
> -	  else
> +	  struct scratch_buffer buf;
> +	  scratch_buffer_init (&buf);
> +	  if (!scratch_buffer_set_array_size (&buf, 1, varlen))
>  	    {
> -	      new_value = malloc (varlen);
> -	      if (new_value == NULL)
> -		{
> -		  UNLOCK;
> -		  return -1;
> -		}
> +	      UNLOCK;
> +	      return -1;
>  	    }
> +	  char *new_value = buf.data;
>  # ifdef _LIBC
>  	  __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
>  		     value, vallen);
> @@ -209,18 +206,9 @@ __add_to_environ (const char *name, const char *value, const char *combined,
>  #endif
>  	    {
>  #ifdef USE_TSEARCH
> -	      if (__glibc_unlikely (! use_alloca))
> -		np = new_value;
> -	      else
> +	      np = new_value;
>  #endif
>  		{
> -		  np = malloc (varlen);
> -		  if (__glibc_unlikely (np == NULL))
> -		    {
> -		      UNLOCK;
> -		      return -1;
> -		    }
> -
>  #ifdef USE_TSEARCH
>  		  memcpy (np, new_value, varlen);
>  #else
> @@ -234,10 +222,7 @@ __add_to_environ (const char *name, const char *value, const char *combined,
>  	    }
>  #ifdef USE_TSEARCH
>  	  else
> -	    {
> -	      if (__glibc_unlikely (! use_alloca))
> -		free (new_value);
> -	    }
> +	    scratch_buffer_free (&buf);
>  #endif
>  	}
>  

  parent reply	other threads:[~2023-06-29 14:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-23 15:25 Joe Simmons-Talbott
2023-06-26 13:12 ` Carlos O'Donell
2023-06-29 14:39 ` Adhemerval Zanella Netto [this message]
2023-06-29 15:25   ` Joe Simmons-Talbott

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=afe9f92d-8fdc-7f61-9ea2-5b5403c573f7@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=josimmon@redhat.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).