From patchwork Mon Mar 2 08:36:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Simek X-Patchwork-Id: 243160 List-Id: U-Boot discussion From: michal.simek at xilinx.com (Michal Simek) Date: Mon, 2 Mar 2020 09:36:27 +0100 Subject: [PATCH v2] lib: Improve _parse_integer_fixup_radix base 16 detection Message-ID: <3435e8a9a13f8ae6b68c2264ed787293a4fa483e.1583138185.git.michal.simek@xilinx.com> Base autodetection is failing for this case: if test 257 -gt 3ae; then echo first; else echo second; fi It is because base for 3ae is recognized by _parse_integer_fixup_radix() as 10. The patch is checking all chars to make sure that they are not 'a' or up. If they are base needs to be in hex. Signed-off-by: Michal Simek Signed-off-by: Shiril Tichkule Reviewed-by: Tom Rini --- Changes in v2: - Fix end of string to be \0 instead of \n - Detect hex by checking chars between a-f to avoid cases like number follow by ; lib/strto.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/strto.c b/lib/strto.c index 55ff9f7437d5..4ac79f46a5e2 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -22,9 +22,26 @@ static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) *base = 16; else *base = 8; - } else + } else { + int i; + *base = 10; + + for (i = 0; ; i++) { + char var = s[i]; + + if (var == '\0') + break; + + if ((var >= 'a' && var <= 'f') || + (var >= 'A' && var <= 'F')) { + *base = 16; + break; + } + } + } } + if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x') s += 2; return s;