[PATCH v2 14/14] RFC: Change simple_strtoul() et al to default to hex

Simon Glass sjg at chromium.org
Sat Jul 24 17:03:41 CEST 2021


Most U-Boot commands use hex for numeric values. However there are quite a
few places where a base of '0' is given, meaning to use the default.

At present the default input base is actually decimal, which seems
confusing. Change this to hex.

NOTE: This is a breaking change, for discussion only. It needs more
thought and careful checking of each use. Do not apply.

Also there is no documentation update here, since it is not yet clear what
to say.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

(no changes since v1)

 include/vsprintf.h |  4 ++--
 lib/strto.c        |  4 ++--
 test/str_ut.c      | 13 +++++++------
 3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/include/vsprintf.h b/include/vsprintf.h
index 85879e7a9e0..bc2e2f6f483 100644
--- a/include/vsprintf.h
+++ b/include/vsprintf.h
@@ -34,7 +34,7 @@
  * implemented since it is rarely used. See the 'setexpr' command for more
  * options.
  *
- * If @base is 0 it defaults to decimal (10).
+ * If @base is 0 it defaults to hex (16).
  */
 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
 
@@ -95,7 +95,7 @@ unsigned long dectoul(const char *cp, char **endp);
  * options.
  *
  *
- * If @base is 0 it defaults to decimal (10).
+ * If @base is 0 it defaults to hex (16).
  *
  * Copied this function from Linux 2.6.38 commit ID:
  * 521cb40b0c44418a4fd36dc633f575813d59a43d
diff --git a/lib/strto.c b/lib/strto.c
index 2168d717b25..9126724e129 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -29,9 +29,9 @@ static const char *_parse_integer_fixup_radix(const char *s, uint *basep)
 			s -= 2;  /* odd, nothing found */
 	}
 
-	/* Use decimal by default */
+	/* Use hex by default */
 	if (!*basep)
-		*basep = 10;
+		*basep = 16;
 
 	return s;
 }
diff --git a/test/str_ut.c b/test/str_ut.c
index 26fc0864dec..4d22bdd24fd 100644
--- a/test/str_ut.c
+++ b/test/str_ut.c
@@ -94,19 +94,20 @@ static int str_simple_strtoul(struct unit_test_state *uts)
 
 		/* Base 0 */
 		ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
-		ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
+		ut_assertok(run_strtoul(uts, str2, 0, 0x1099ab, 6, upper));
 		ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
 
 		/* Base 2 */
 		ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
 		ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
+		ut_assertok(run_strtoul(uts, str3, 2, 0xb, 3, upper));
 	}
 
 	/* Check endp being NULL */
-	ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
+	ut_asserteq(0x1099ab, simple_strtoul(str2, NULL, 0));
 
 	/* check decimal */
-	ut_assertok(run_strtoul(uts, "123fg", 0, 123, 3, false));
+	ut_assertok(run_strtoul(uts, "123fg", 0, 0x123f, 4, false));
 	ut_assertok(run_strtoul(uts, "123a", 10, 123, 3, false));
 	ut_assertok(run_strtoul(uts, "0x123fg", 0, 0x123f, 6, false));
 	ut_assertok(run_strtoul(uts, "0n123a", 16, 123, 5, false));
@@ -159,7 +160,7 @@ static int str_simple_strtoull(struct unit_test_state *uts)
 
 		/* Base 0 */
 		ut_assertok(run_strtoull(uts, str1, 0, 0, 0, upper));
-		ut_assertok(run_strtoull(uts, str2, 0, 1099, 4, upper));
+		ut_assertok(run_strtoull(uts, str2, 0, 0x1099ab, 6, upper));
 		ut_assertok(run_strtoull(uts, str3, 0, 0xb, 3, upper));
 
 		/* Base 2 */
@@ -168,10 +169,10 @@ static int str_simple_strtoull(struct unit_test_state *uts)
 	}
 
 	/* Check endp being NULL */
-	ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
+	ut_asserteq(0x1099ab, simple_strtoull(str2, NULL, 0));
 
 	/* check decimal */
-	ut_assertok(run_strtoull(uts, "123fg", 0, 123, 3, false));
+	ut_assertok(run_strtoull(uts, "123fg", 0, 0x123f, 4, false));
 	ut_assertok(run_strtoull(uts, "123a", 10, 123, 3, false));
 	ut_assertok(run_strtoull(uts, "0x123fg", 0, 0x123f, 6, false));
 	ut_assertok(run_strtoull(uts, "0n123a", 16, 123, 5, false));
-- 
2.32.0.432.gabb21c7263-goog



More information about the U-Boot mailing list