lib: strto: fix metric suffix parsing in strtoul[l]
authorMiquel Raynal <miquel.raynal@bootlin.com>
Thu, 6 Sep 2018 07:08:44 +0000 (09:08 +0200)
committerJagan Teki <jagan@amarulasolutions.com>
Thu, 20 Sep 2018 14:41:01 +0000 (20:11 +0530)
While 1kB or 1kiB will be parsed correctly, 1k will return the right
amount, but the metric suffix will not be escaped once the char
pointer updated. Fix this situation by simplifying the move of the
endp pointer.

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

index b7fc31d..55ff9f7 100644 (file)
@@ -94,12 +94,11 @@ unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
                /* fall through */
        case 'k':
                result *= 1024;
-               if ((*endp)[1] == 'i') {
-                       if ((*endp)[2] == 'B')
-                               (*endp) += 3;
-                       else
-                               (*endp) += 2;
-               }
+               (*endp)++;
+               if (**endp == 'i')
+                       (*endp)++;
+               if (**endp == 'B')
+                       (*endp)++;
        }
        return result;
 }
@@ -116,12 +115,11 @@ unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
                /* fall through */
        case 'k':
                result *= 1024;
-               if ((*endp)[1] == 'i') {
-                       if ((*endp)[2] == 'B')
-                               (*endp) += 3;
-                       else
-                               (*endp) += 2;
-               }
+               (*endp)++;
+               if (**endp == 'i')
+                       (*endp)++;
+               if (**endp == 'B')
+                       (*endp)++;
        }
        return result;
 }