From 3805dfc06eba4f328997ab6cbb0910a7a906fbb0 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Tue, 2 Dec 2014 11:31:20 -0600 Subject: [PATCH] Replace stack-allocated array with dynamically-allocated data in getopt32(). --- libbb/getopt32.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libbb/getopt32.c b/libbb/getopt32.c index 2a75357fb..428de534d 100644 --- a/libbb/getopt32.c +++ b/libbb/getopt32.c @@ -299,6 +299,7 @@ Special characters: */ /* Code here assumes that 'unsigned' is at least 32 bits wide */ +/* This is only important if applet_opts is over 16 chars long. */ const char *const bb_argv_dash[] = { "-", NULL }; @@ -337,7 +338,7 @@ getopt32(char **argv, const char *applet_opts, ...) int argc; unsigned flags = 0; unsigned requires = 0; - t_complementary complementary[33]; /* last stays zero-filled */ + t_complementary *complementary = NULL; /* last stays zero-filled */ char first_char; int c; const unsigned char *s; @@ -365,15 +366,17 @@ getopt32(char **argv, const char *applet_opts, ...) va_start(p, applet_opts); - c = 0; - on_off = complementary; - memset(on_off, 0, sizeof(complementary)); - /* skip bbox extension */ first_char = applet_opts[0]; if (first_char == '!') applet_opts++; + c = 0; + complementary = calloc(33, sizeof(*complementary)); + if (complementary == NULL) + goto error; + on_off = complementary; + /* skip GNU extension */ s = (const unsigned char *)applet_opts; if (*s == '+' || *s == '-') @@ -612,10 +615,12 @@ getopt32(char **argv, const char *applet_opts, ...) goto error; option_mask32 = flags; + free(complementary); return flags; error: if (first_char != '!') bb_show_usage(); + free(complementary); return (int32_t)-1; }