]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
util: reduce allocation sizes in argconfig_parse()
authorCaleb Sander Mateos <csander@purestorage.com>
Tue, 16 Jul 2024 19:07:23 +0000 (13:07 -0600)
committerDaniel Wagner <wagi@monom.org>
Wed, 17 Jul 2024 14:57:45 +0000 (16:57 +0200)
long_opts stores up to 1 element per option, plus a help element,
and a zerored final element. So it can be allocated with length
options_count + 2 instead of options_count + 3.
short_opts stores up to 3 characters per option, plus 2 help characters,
and a NUL terminator. So it can be allocated with length
options_count * 3 + 3 instead of options_count * 3 + 5.

Also use the two arguments to calloc() instead of multiplying the sizes.

Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
util/argconfig.c

index 9a416376cb54c77e644109c35b0b67d057e2ef57..5cd5889a673e5bc34eb2f500c3f494c151d3845b 100644 (file)
@@ -303,8 +303,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
        for (s = options; s->option; s++)
                options_count++;
 
-       long_opts = calloc(1, sizeof(struct option) * (options_count + 3));
-       short_opts = calloc(1, sizeof(*short_opts) * (options_count * 3 + 5));
+       long_opts = calloc(options_count + 2, sizeof(struct option));
+       short_opts = calloc(options_count * 3 + 3, sizeof(*short_opts));
 
        if (!long_opts || !short_opts) {
                fprintf(stderr, "failed to allocate memory for opts: %s\n", strerror(errno));