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>
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));