public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Joe Simmons-Talbott <josimmon@redhat.com>
To: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] getaddrinfo: Get rid of alloca
Date: Fri, 30 Jun 2023 10:42:21 -0400	[thread overview]
Message-ID: <20230630144221.GS6392@oak> (raw)
In-Reply-To: <cf85b6e3-b8a3-5f18-6c8b-84dc26e39c40@linaro.org>

On Fri, Jun 30, 2023 at 10:15:48AM -0300, Adhemerval Zanella Netto wrote:
> 
> 
> On 20/06/23 18:40, Joe Simmons-Talbott via Libc-alpha wrote:
> > Use a scratch_buffer rather than alloca to avoid potential stack
> > overflow.
> > ---
> >  sysdeps/posix/getaddrinfo.c | 22 ++++++++--------------
> >  1 file changed, 8 insertions(+), 14 deletions(-)
> > 
> > diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
> > index 0356b622be..442475d621 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, 1, alloc_size))
> 
> I think it would be better to use:
> 
>   if (!scratch_buffer_set_array_size (&buf, nresults, 
> 				      sizeof (*results) + sizeof (size_t)))
>     [...]
> 
> To use the overflow checks done by scratch_buffer_set_array_size (
> sizeof (*results) + sizeof (size_t) is always safe so I think there is no
> need to add extra checks).

You are correct.  Thanks for catching that and for the review.  I've
pushed your suggestion as v2.

Thanks,
Joe


      reply	other threads:[~2023-06-30 14:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-20 21:40 Joe Simmons-Talbott
2023-06-30 13:15 ` Adhemerval Zanella Netto
2023-06-30 14:42   ` Joe Simmons-Talbott [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=20230630144221.GS6392@oak \
    --to=josimmon@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --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).