From a353e6aa8ec8f0aa101cc7e9543fe7843ddc6d98 Mon Sep 17 00:00:00 2001 From: Miquel Raynal Date: Thu, 6 Sep 2018 09:08:43 +0200 Subject: lib: strto: parse all lowercase metric prefixes in ustrtoul[l] 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 Reviewed-by: Stefan Roese --- lib/strto.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/strto.c b/lib/strto.c index 7f6076909a..b7fc31d6e5 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -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') { -- cgit v1.2.3 From b87b0d8d79e05f85e4f18200eac4283d513c3f3c Mon Sep 17 00:00:00 2001 From: Miquel Raynal Date: Thu, 6 Sep 2018 09:08:44 +0200 Subject: lib: strto: fix metric suffix parsing in strtoul[l] 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 Reviewed-by: Stefan Roese --- lib/strto.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/strto.c b/lib/strto.c index b7fc31d6e5..55ff9f7437 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -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; } -- cgit v1.2.3