Skip to content

Commit

Permalink
lib: strto: fix incorrect handling of specified base
Browse files Browse the repository at this point in the history
The strto functions should honor the specified base (if non-zero) rather
than permitting a hex or octal string when the user wanted (for example)
base 10.

This has been fixed somewhere along the way in the upstream linux kernel
src tree, at some point after these was copied in to u-boot.  And also
in a way that duplicates less code.  So port _parse_integer_fixup_radix()
to u-boot.

Signed-off-by: Rob Clark <[email protected]>
Reviewed-by: Simon Glass <[email protected]>
  • Loading branch information
robclark authored and trini committed Sep 15, 2017
1 parent c577218 commit 2e79461
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions lib/strto.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,30 @@
#include <errno.h>
#include <linux/ctype.h>

/* from lib/kstrtox.c */
static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
{
if (*base == 0) {
if (s[0] == '0') {
if (tolower(s[1]) == 'x' && isxdigit(s[2]))
*base = 16;
else
*base = 8;
} else
*base = 10;
}
if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
s += 2;
return s;
}

unsigned long simple_strtoul(const char *cp, char **endp,
unsigned int base)
{
unsigned long result = 0;
unsigned long value;

if (*cp == '0') {
cp++;
if ((*cp == 'x') && isxdigit(cp[1])) {
base = 16;
cp++;
}

if (!base)
base = 8;
}

if (!base)
base = 10;
cp = _parse_integer_fixup_radix(cp, &base);

while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
? toupper(*cp) : *cp)-'A'+10) < base) {
Expand Down Expand Up @@ -128,19 +133,7 @@ unsigned long long simple_strtoull(const char *cp, char **endp,
{
unsigned long long result = 0, value;

if (*cp == '0') {
cp++;
if ((*cp == 'x') && isxdigit(cp[1])) {
base = 16;
cp++;
}

if (!base)
base = 8;
}

if (!base)
base = 10;
cp = _parse_integer_fixup_radix(cp, &base);

while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
: (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
Expand Down

0 comments on commit 2e79461

Please sign in to comment.