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

Simon Glass
2021-07-20T13:32:46Z

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@chromium.org>
---

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

diff --git a/include/vsprintf.h b/include/vsprintf.h
index 054e0a4dee3..d3370f9c4e8 100644
--- a/include/vsprintf.h
+++ b/include/vsprintf.h
@@ -31,7 +31,7 @@
* as decimal 123, not octal. This is to avoid confusion with hex values which
* can start with 0.
*
- * 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);

@@ -88,7 +88,7 @@ unsigned long dectoul(const char *cp, char **endp);
* as decimal 123, not octal. This is to avoid confusion with hex values which
* can start with 0.
*
- * 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 066e5748285..267189a35e0 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -33,9 +33,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 90e77d49377..4359ff01bf9 100644
--- a/test/str_ut.c
+++ b/test/str_ut.c
@@ -93,7 +93,7 @@ static int str_simple_strtoul(struct unit_test_state *uts)
ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper));

/* Octal */
- ut_assertok(run_strtoul(uts, str6, 0, 778, 4, upper));
+ ut_assertok(run_strtoul(uts, str6, 0, 0x778, 4, upper));
ut_assertok(run_strtoul(uts, str7, 8, 0x1c7, 3, upper));
ut_assertok(run_strtoul(uts, str8, 0, 63, 4, upper));

@@ -102,19 +102,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, "0m123a", 16, 123, 5, false));
@@ -159,7 +160,7 @@ static int str_simple_strtoull(struct unit_test_state *uts)
ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper));

/* Octal */
- ut_assertok(run_strtoull(uts, str6, 0, 778, 4, upper));
+ ut_assertok(run_strtoull(uts, str6, 0, 0x778, 4, upper));
ut_assertok(run_strtoull(uts, str7, 8, 0x1c7, 3, upper));
ut_assertok(run_strtoull(uts, str8, 0, 63, 4, upper));

@@ -176,7 +177,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 */
@@ -185,10 +186,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, "0m123a", 16, 123, 5, false));
--
2.32.0.402.g57bb445576-goog