From: Joe Simmons-Talbott <josimmon@redhat.com>
To: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v2] argp-help: Get rid of alloca.
Date: Wed, 13 Sep 2023 12:02:04 -0400 [thread overview]
Message-ID: <20230913160204.GO3849957@oak> (raw)
In-Reply-To: <e9019b8e-8868-fddb-2a14-f2f4cfb3a9c5@linaro.org>
On Wed, Sep 13, 2023 at 11:39:10AM -0300, Adhemerval Zanella Netto wrote:
>
>
> On 28/08/23 15:27, Joe Simmons-Talbott via Libc-alpha wrote:
> > Replace alloca with a scratch_buffer to avoid potential stack overflow.
> >
> > Checked on x86_64-linux-gnu
> > ---
> > Changes to v1:
> > * Call assert on allocation failure.
> >
> > argp/argp-help.c | 30 +++++++++++++++++++++++++++---
> > 1 file changed, 27 insertions(+), 3 deletions(-)
> >
> > diff --git a/argp/argp-help.c b/argp/argp-help.c
> > index d019ed58d2..af99649f20 100644
> > --- a/argp/argp-help.c
> > +++ b/argp/argp-help.c
> > @@ -40,6 +40,7 @@ char *alloca ();
> > # endif
> > #endif
> >
> > +#include <scratch_buffer.h>
> > #include <stdbool.h>
> > #include <stddef.h>
> > #include <stdlib.h>
> > @@ -1450,8 +1451,19 @@ hol_usage (struct hol *hol, argp_fmtstream_t stream)
> > {
> > unsigned nentries;
> > struct hol_entry *entry;
> > - char *short_no_arg_opts = alloca (strlen (hol->short_options) + 1);
> > - char *snao_end = short_no_arg_opts;
> > + struct scratch_buffer buf;
> > + scratch_buffer_init (&buf);
> > + char *short_no_arg_opts;
> > + char *snao_end;
> > + bool result;
> > +
> > + result = scratch_buffer_set_array_size (&buf, 1,
> > + strlen (hol->short_options) + 1);
> > + assert (result);
> > +
> > + short_no_arg_opts = buf.data;
> > + snao_end = short_no_arg_opts;
> > +
> >
>
> With a second look, this interface is really not well designed for memory
> allocation failures so I think it would be simpler to just use malloc here
> and assert for allocation failure (It would also make it simpler to eventually
> sync with gnulib).
>
> Also, remove the boilerplate alloca definition above.
Ack. Fixed in v3.
Thanks,
Joe
prev parent reply other threads:[~2023-09-13 16:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-28 18:27 Joe Simmons-Talbott
2023-09-13 13:02 ` Joe Simmons-Talbott
2023-09-13 14:39 ` Adhemerval Zanella Netto
2023-09-13 16:02 ` 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=20230913160204.GO3849957@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).