[PATCH] nvmecontrol: Fix shortopts allocation

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] nvmecontrol: Fix shortopts allocation

Sebastian Huber-4
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));
  if (shortopts == NULL)
  err(1, "shortopts memory");
  idx = 0;
--
2.16.4

_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] nvmecontrol: Fix shortopts allocation

Warner Losh
On Wed, Sep 11, 2019 at 2:28 AM Sebastian Huber <
[hidden email]> 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 :.


>         if (shortopts == NULL)
>                 err(1, "shortopts memory");
>         idx = 0;
> --
> 2.16.4
>
> _______________________________________________
> [hidden email] mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "[hidden email]"
>
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] nvmecontrol: Fix shortopts allocation

Sebastian Huber-4
On 11/09/2019 15:30, Warner Losh wrote:

>
>
> On Wed, Sep 11, 2019 at 2:28 AM Sebastian Huber
> <[hidden email]
> <mailto:[hidden email]>> 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  : [hidden email]
PGP     : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[hidden email]"