[PATCH] nvmecontrol: Fix shortopts allocation
Sebastian Huber
sebastian.huber at embedded-brains.de
Fri Sep 13 05:53:51 UTC 2019
On 11/09/2019 15:30, Warner Losh wrote:
>
>
> On Wed, Sep 11, 2019 at 2:28 AM Sebastian Huber
> <sebastian.huber at embedded-brains.de
> <mailto:sebastian.huber at embedded-brains.de>> wrote:
>
> Account for potential ':' in the short options string.
> ---
> sbin/nvmecontrol/comnd.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/sbin/nvmecontrol/comnd.c b/sbin/nvmecontrol/comnd.c
> index d8d97ad11b5..a91969ff766 100644
> --- a/sbin/nvmecontrol/comnd.c
> +++ b/sbin/nvmecontrol/comnd.c
> @@ -189,7 +189,7 @@ arg_parse(int argc, char * const * argv, const
> struct cmd *f)
> lopts = malloc((n + 2) * sizeof(struct option));
> if (lopts == NULL)
> err(1, "option memory");
> - p = shortopts = malloc((n + 3) * sizeof(char));
> + p = shortopts = malloc((2 * n + 2) * sizeof(char));
>
>
> I'll apply, but I still think this needs to be + 3 to account for the
> NUL at the end (even though we kinda know that the 2 options we're
> adding don't have :.
Thanks for checking it in. I wondered why there was a + 3, we have in
arg_parse() with unrelated code omitted:
p = shortopts = malloc((2 * n + 3) * sizeof(char));
for (i = 0; i < n; i++) {
if (isprint(opts[i].short_arg)) {
*p++ = opts[i].short_arg;
if (lopts[i].has_arg)
*p++ = ':';
}
}
*p++ = '?';
*p++ = '\0';
while ((ch = getopt_long(argc, argv, shortopts, lopts, &idx)) != -1) {
Looks like the last ++ is superfluous.
--
Sebastian Huber, embedded brains GmbH
Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail : sebastian.huber at embedded-brains.de
PGP : Public key available on request.
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
More information about the freebsd-hackers
mailing list