Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Sep 2019 07:53:43 +0200
From:      Sebastian Huber <sebastian.huber@embedded-brains.de>
To:        Warner Losh <imp@bsdimp.com>
Cc:        "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>
Subject:   Re: [PATCH] nvmecontrol: Fix shortopts allocation
Message-ID:  <ad23539b-92f1-445d-0d79-30fbfa2fb0c8@embedded-brains.de>
In-Reply-To: <CANCZdfo_L9AuEqV0ur0xT71vxUX3d8UGhW9kz6mo5AXkP=FJ6w@mail.gmail.com>
References:  <20190911082833.32388-1-sebastian.huber@embedded-brains.de> <CANCZdfo_L9AuEqV0ur0xT71vxUX3d8UGhW9kz6mo5AXkP=FJ6w@mail.gmail.com>

index | next in thread | previous in thread | raw e-mail

On 11/09/2019 15:30, Warner Losh wrote:
> 
> 
> On Wed, Sep 11, 2019 at 2:28 AM Sebastian Huber 
> <sebastian.huber@embedded-brains.de 
> <mailto:sebastian.huber@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@embedded-brains.de
PGP     : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.


help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?ad23539b-92f1-445d-0d79-30fbfa2fb0c8>