]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: Add helper function to parse 16-bit comma separated list
authorFrancis Pravin Antony Michael Raj <francis.michael@solidigm.com>
Sun, 14 Aug 2022 02:30:08 +0000 (22:30 -0400)
committerFrancis Pravin Antony Michael Raj <francis.michael@solidigm.com>
Sun, 14 Aug 2022 02:30:08 +0000 (22:30 -0400)
In copy command, nlbs is declared as __u16. While parsing command line argument,
it is typecast to integer pointer. So nlbs[1], nlbs[3], nlbs[5], ... are stored
as zero after getting parsed. Therefore it is passing wrong value to controller.
Hence, adding a helper function to parse the 16-bit comma separated list

Signed-off-by: Francis Pravin Antony Michael Raj <francis.michael@solidigm.com>
Signed-off-by: Jonathan Derrick <jonathan.derrick@solidigm.com>
nvme.c
util/argconfig.c
util/argconfig.h

diff --git a/nvme.c b/nvme.c
index 4096a91ead0f2475474318188f3bb260fa338045..12d02820e678488f423dfb66bca431ff0b7f0552 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -5795,7 +5795,7 @@ static int copy(int argc, char **argv, struct command *cmd, struct plugin *plugi
                goto ret;
        }
 
-       nb = argconfig_parse_comma_sep_array(cfg.nlbs, (int *)nlbs, ARRAY_SIZE(nlbs));
+       nb = argconfig_parse_comma_sep_array_short(cfg.nlbs, nlbs, ARRAY_SIZE(nlbs));
        ns = argconfig_parse_comma_sep_array_long(cfg.slbas, slbas, ARRAY_SIZE(slbas));
        
        if (cfg.format == 0)
index d09ba9de0d92c299da301ec334b343353491f1a0..2c328ffefd7a643479fa0a5a2adac3cdf40294ca 100644 (file)
@@ -488,6 +488,51 @@ int argconfig_parse_comma_sep_array(char *string, int *val,
        }
 }
 
+int argconfig_parse_comma_sep_array_short(char *string, unsigned short *val,
+                                         unsigned max_length)
+{
+       int ret = 0;
+       unsigned long v;
+       char *tmp;
+       char *p;
+
+       if (!string || !strlen(string))
+               return 0;
+
+       tmp = strtok(string, ",");
+       if (!tmp)
+               return 0;
+
+       v = strtoul(tmp, &p, 0);
+       if (*p != 0)
+               return -1;
+       if (v > UINT16_MAX) {
+               fprintf(stderr, "%s out of range\n", tmp);
+               return -1;
+       }
+       val[ret] = v;
+       ret++;
+
+       while (1) {
+               tmp = strtok(NULL, ",");
+               if (tmp == NULL)
+                       return ret;
+
+               if (ret >= max_length)
+                       return -1;
+
+               v = strtoul(tmp, &p, 0);
+               if (*p != 0)
+                       return -1;
+               if (v > UINT16_MAX) {
+                       fprintf(stderr, "%s out of range\n", tmp);
+                       return -1;
+               }
+               val[ret] = v;
+               ret++;
+       }
+}
+
 int argconfig_parse_comma_sep_array_long(char *string,
                                              unsigned long long *val,
                                              unsigned max_length)
index de715b12a5296b0de9e7341bc7e3eb9a01e887af..b66706a699d8b6ea2ad1e6f120b6d5a0ab63a94d 100644 (file)
@@ -126,6 +126,8 @@ int argconfig_parse_subopt_string(char *string, char **options,
                                  size_t max_options);
 int argconfig_parse_comma_sep_array(char *string, int *ret,
                                         unsigned max_length);
+int argconfig_parse_comma_sep_array_short(char *string, unsigned short *ret,
+                                         unsigned max_length);
 int argconfig_parse_comma_sep_array_long(char *string,
                                              unsigned long long *ret,
                                              unsigned max_length);