lib: strto: parse all lowercase metric prefixes in ustrtoul[l]
authorMiquel Raynal <miquel.raynal@bootlin.com>
Thu, 6 Sep 2018 07:08:43 +0000 (09:08 +0200)
committerJagan Teki <jagan@amarulasolutions.com>
Thu, 20 Sep 2018 14:41:01 +0000 (20:11 +0530)
Both ustrtoul and ustrtoull interpret 1k but not 1m or 1g. Even if the
SI symbols for Mega and Giga are 'M' and 'G', certain entries of
eg. mtdparts also use (wrongly) the metric prefix 'm' and 'g'.

I do not see how parsing lowercase prefixes could break anything, so
parse them like their uppercase counterpart.

Also, even though kiB is not equal to kB in general, lets not change
U-Boot behavior and always use kiB and kB (same applies for MiB vs. MB
and GiB vs. GB) as a representation for 1024 instead of 1000.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Reviewed-by: Stefan Roese <sr@denx.de>
lib/strto.c

index 7f60769..b7fc31d 100644 (file)
@@ -85,14 +85,13 @@ long simple_strtol(const char *cp, char **endp, unsigned int base)
 unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
 {
        unsigned long result = simple_strtoul(cp, endp, base);
-       switch (**endp) {
-       case 'G':
+       switch (tolower(**endp)) {
+       case 'g':
                result *= 1024;
                /* fall through */
-       case 'M':
+       case 'm':
                result *= 1024;
                /* fall through */
-       case 'K':
        case 'k':
                result *= 1024;
                if ((*endp)[1] == 'i') {
@@ -108,14 +107,13 @@ unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
 unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
 {
        unsigned long long result = simple_strtoull(cp, endp, base);
-       switch (**endp) {
-       case 'G':
+       switch (tolower(**endp)) {
+       case 'g':
                result *= 1024;
                /* fall through */
-       case 'M':
+       case 'm':
                result *= 1024;
                /* fall through */
-       case 'K':
        case 'k':
                result *= 1024;
                if ((*endp)[1] == 'i') {