public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] [PATCH] lib: strto: fix incorrect handling of specified base
@ 2017-09-11 20:53 Rob Clark
  2017-09-12 12:27 ` Simon Glass
  2017-09-15 12:30 ` [U-Boot] " Tom Rini
  0 siblings, 2 replies; 3+ messages in thread
From: Rob Clark @ 2017-09-11 20:53 UTC (permalink / raw)
  To: u-boot

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 <robdclark@gmail.com>
---
 lib/strto.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/lib/strto.c b/lib/strto.c
index e93a4f5491..7f6076909a 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -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) {
@@ -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) {
-- 
2.13.5

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [U-Boot] [PATCH] lib: strto: fix incorrect handling of specified base
  2017-09-11 20:53 [U-Boot] [PATCH] lib: strto: fix incorrect handling of specified base Rob Clark
@ 2017-09-12 12:27 ` Simon Glass
  2017-09-15 12:30 ` [U-Boot] " Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Simon Glass @ 2017-09-12 12:27 UTC (permalink / raw)
  To: u-boot

On 11 September 2017 at 14:53, Rob Clark <robdclark@gmail.com> wrote:
>
> 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 <robdclark@gmail.com>
> ---
>  lib/strto.c | 45 +++++++++++++++++++--------------------------
>  1 file changed, 19 insertions(+), 26 deletions(-)
>

Reviewed-by: Simon Glass <sjg@chromium.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [U-Boot] lib: strto: fix incorrect handling of specified base
  2017-09-11 20:53 [U-Boot] [PATCH] lib: strto: fix incorrect handling of specified base Rob Clark
  2017-09-12 12:27 ` Simon Glass
@ 2017-09-15 12:30 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Rini @ 2017-09-15 12:30 UTC (permalink / raw)
  To: u-boot

On Mon, Sep 11, 2017 at 04:53:08PM -0400, Rob Clark wrote:

> 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 <robdclark@gmail.com>
> Reviewed-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20170915/0f54ef15/attachment.sig>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-09-15 12:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-09-11 20:53 [U-Boot] [PATCH] lib: strto: fix incorrect handling of specified base Rob Clark
2017-09-12 12:27 ` Simon Glass
2017-09-15 12:30 ` [U-Boot] " Tom Rini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox