]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
provide value bounds check on numerical lists
authorKeith Busch <kbusch@kernel.org>
Fri, 30 Oct 2020 21:28:16 +0000 (14:28 -0700)
committerKeith Busch <kbusch@kernel.org>
Fri, 30 Oct 2020 21:30:57 +0000 (14:30 -0700)
Check if the user provided a value exceeding the 32 bits they're
going to be used with rather than silently truncate the value.

Link: https://github.com/linux-nvme/nvme-cli/issues/828
Signed-off-by: Keith Busch <kbusch@kernel.org>
util/argconfig.c

index f647448a14617aa3c71faea3bf2b21944af0ad2e..d289298c0c997569e2976dffa6fa727b7b69b9ce 100644 (file)
 #include "argconfig.h"
 #include "suffix.h"
 
-#include <string.h>
+#include <errno.h>
+#include <inttypes.h>
 #include <getopt.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
 #include <stdarg.h>
-#include <inttypes.h>
+#include <string.h>
 
 static argconfig_help_func *help_funcs[MAX_HELP_FUNC] = { NULL };
 
@@ -454,6 +455,7 @@ unsigned argconfig_parse_comma_sep_array(char *string, int *val,
                                         unsigned max_length)
 {
        unsigned ret = 0;
+       unsigned long v;
        char *tmp;
        char *p;
 
@@ -464,9 +466,14 @@ unsigned argconfig_parse_comma_sep_array(char *string, int *val,
        if (!tmp)
                return 0;
 
-       val[ret] = strtol(tmp, &p, 0);
+       v = strtoul(tmp, &p, 0);
        if (*p != 0)
                return -1;
+       if (v > UINT_MAX) {
+               fprintf(stderr, "%s out of range\n", tmp);
+               return -1;
+       }
+       val[ret] = v;
 
        ret++;
        while (1) {
@@ -478,10 +485,14 @@ unsigned argconfig_parse_comma_sep_array(char *string, int *val,
                if (ret >= max_length)
                        return -1;
 
-               val[ret] = strtol(tmp, &p, 0);
-
+               v = strtoul(tmp, &p, 0);
                if (*p != 0)
                        return -1;
+               if (v > UINT_MAX) {
+                       fprintf(stderr, "%s out of range\n", tmp);
+                       return -1;
+               }
+               val[ret] = v;
                ret++;
        }
 }