Add check for OPT_LEN_MAX being too small
[fio.git] / parse.c
diff --git a/parse.c b/parse.c
index 0bf28a5bd72463d574560d828859669b9b0122c2..d44d13082c91dd8951cbe0be7edf2c51dd73d7e0 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -14,7 +14,7 @@
 #include "debug.h"
 
 static struct fio_option *fio_options;
-extern unsigned int fio_kb_base;
+extern unsigned int fio_get_kb_base(void *);
 
 static int vp_cmp(const void *p1, const void *p2)
 {
@@ -113,8 +113,9 @@ static unsigned long get_mult_time(char c)
        }
 }
 
-static unsigned long long get_mult_bytes(char c)
+static unsigned long long get_mult_bytes(char c, void *data)
 {
+       unsigned int kb_base = fio_get_kb_base(data);
        unsigned long long ret = 1;
 
        switch (c) {
@@ -122,19 +123,19 @@ static unsigned long long get_mult_bytes(char c)
                break;
        case 'p':
        case 'P':
-               ret *= (unsigned long long) fio_kb_base;
+               ret *= (unsigned long long) kb_base;
        case 't':
        case 'T':
-               ret *= (unsigned long long) fio_kb_base;
+               ret *= (unsigned long long) kb_base;
        case 'g':
        case 'G':
-               ret *= (unsigned long long) fio_kb_base;
+               ret *= (unsigned long long) kb_base;
        case 'm':
        case 'M':
-               ret *= (unsigned long long) fio_kb_base;
+               ret *= (unsigned long long) kb_base;
        case 'k':
        case 'K':
-               ret *= (unsigned long long) fio_kb_base;
+               ret *= (unsigned long long) kb_base;
                break;
        }
 
@@ -144,7 +145,7 @@ static unsigned long long get_mult_bytes(char c)
 /*
  * convert string into decimal value, noting any size suffix
  */
-int str_to_decimal(const char *str, long long *val, int kilo)
+int str_to_decimal(const char *str, long long *val, int kilo, void *data)
 {
        int len, base;
 
@@ -161,22 +162,32 @@ int str_to_decimal(const char *str, long long *val, int kilo)
        if (*val == LONG_MAX && errno == ERANGE)
                return 1;
 
-       if (kilo)
-               *val *= get_mult_bytes(str[len - 1]);
-       else
+       if (kilo) {
+               const char *p;
+               /*
+                * if the last char is 'b' or 'B', the user likely used
+                * "1gb" instead of just "1g". If the second to last is also
+                * a letter, adjust.
+                */
+               p = str + len - 1;
+               if ((*p == 'b' || *p == 'B') && isalpha(*(p - 1)))
+                       --p;
+
+               *val *= get_mult_bytes(*p, data);
+       } else
                *val *= get_mult_time(str[len - 1]);
 
        return 0;
 }
 
-static int check_str_bytes(const char *p, long long *val)
+static int check_str_bytes(const char *p, long long *val, void *data)
 {
-       return str_to_decimal(p, val, 1);
+       return str_to_decimal(p, val, 1, data);
 }
 
 static int check_str_time(const char *p, long long *val)
 {
-       return str_to_decimal(p, val, 0);
+       return str_to_decimal(p, val, 0, NULL);
 }
 
 void strip_blank_front(char **p)
@@ -209,7 +220,7 @@ void strip_blank_end(char *p)
        *(s + 1) = '\0';
 }
 
-static int check_range_bytes(const char *str, long *val)
+static int check_range_bytes(const char *str, long *val, void *data)
 {
        char suffix;
 
@@ -217,7 +228,7 @@ static int check_range_bytes(const char *str, long *val)
                return 1;
 
        if (sscanf(str, "%lu%c", val, &suffix) == 2) {
-               *val *= get_mult_bytes(suffix);
+               *val *= get_mult_bytes(suffix, data);
                return 0;
        }
 
@@ -318,7 +329,7 @@ static int __handle_option(struct fio_option *o, const char *ptr, void *data,
                if (is_time)
                        ret = check_str_time(ptr, &ull);
                else
-                       ret = check_str_bytes(ptr, &ull);
+                       ret = check_str_bytes(ptr, &ull, data);
 
                if (ret)
                        break;
@@ -385,8 +396,8 @@ static int __handle_option(struct fio_option *o, const char *ptr, void *data,
                p1 = tmp;
 
                ret = 1;
-               if (!check_range_bytes(p1, &ul1) &&
-                   !check_range_bytes(p2, &ul2)) {
+               if (!check_range_bytes(p1, &ul1, data) &&
+                   !check_range_bytes(p2, &ul2, data)) {
                        ret = 0;
                        if (ul1 > ul2) {
                                unsigned long foo = ul1;
@@ -556,7 +567,7 @@ static int opt_cmp(const void *p1, const void *p2)
 
        o1 = get_option(s1, fio_options, &foo);
        o2 = get_option(s2, fio_options, &foo);
-       
+
        prio1 = prio2 = 0;
        if (o1)
                prio1 = o1->prio;
@@ -609,6 +620,11 @@ static char *option_dup_subs(const char *opt)
        ssize_t nchr = OPT_LEN_MAX;
        size_t envlen;
 
+       if (strlen(in) + 1 > OPT_LEN_MAX) {
+               fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
+               return NULL;
+       }
+
        in[OPT_LEN_MAX] = '\0';
        strncpy(in, opt, OPT_LEN_MAX);
 
@@ -648,6 +664,8 @@ int parse_option(const char *opt, struct fio_option *options, void *data)
        char *post, *tmp;
 
        tmp = option_dup_subs(opt);
+       if (!tmp)
+               return 1;
 
        o = get_option(tmp, options, &post);
        if (!o) {