From: Joe Simmons-Talbott <josimmon@redhat.com>
To: libc-alpha@sourceware.org
Cc: Joe Simmons-Talbott <josimmon@redhat.com>
Subject: [PATCH] argp-help: Get rid of alloca.
Date: Wed, 12 Jul 2023 13:02:06 -0400 [thread overview]
Message-ID: <20230712170206.3675587-1-josimmon@redhat.com> (raw)
Replace alloca with a scratch_buffer to avoid potential stack overflow.
Checked on x86_64-linux-gnu
---
argp/argp-help.c | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)
diff --git a/argp/argp-help.c b/argp/argp-help.c
index d019ed58d2..a5982334f6 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,17 @@ 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;
+
+ if (!scratch_buffer_set_array_size (&buf, 1,
+ strlen (hol->short_options) + 1))
+ return;
+ short_no_arg_opts = buf.data;
+ snao_end = short_no_arg_opts;
+
/* First we put a list of short options without arguments. */
for (entry = hol->entries, nentries = hol->num_entries
@@ -1478,6 +1488,8 @@ hol_usage (struct hol *hol, argp_fmtstream_t stream)
; entry++, nentries--)
hol_entry_long_iterate (entry, usage_long_opt,
entry->argp->argp_domain, stream);
+
+ scratch_buffer_free (&buf);
}
}
\f
@@ -1698,7 +1710,13 @@ _help (const struct argp *argp, const struct argp_state *state, FILE *stream,
{
int first_pattern = 1, more_patterns;
size_t num_pattern_levels = argp_args_levels (argp);
- char *pattern_levels = alloca (num_pattern_levels);
+ struct scratch_buffer buf;
+ scratch_buffer_init (&buf);
+ char *pattern_levels;
+
+ if (!scratch_buffer_set_array_size (&buf, 1, num_pattern_levels))
+ return;
+ pattern_levels = buf.data;
memset (pattern_levels, 0, num_pattern_levels);
@@ -1746,6 +1764,8 @@ _help (const struct argp *argp, const struct argp_state *state, FILE *stream,
first_pattern = 0;
}
while (more_patterns);
+
+ scratch_buffer_free (&buf);
}
if (flags & ARGP_HELP_PRE_DOC)
--
2.39.2
next reply other threads:[~2023-07-12 17:02 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-12 17:02 Joe Simmons-Talbott [this message]
2023-08-03 13:11 ` Joe Simmons-Talbott
2023-08-15 14:49 ` Joe Simmons-Talbott
2023-08-28 13:20 ` Joe Simmons-Talbott
2023-08-28 16:52 ` Adhemerval Zanella Netto
2023-08-28 18:29 ` 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=20230712170206.3675587-1-josimmon@redhat.com \
--to=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).