[PATCH v2 13/14] lib: Drop octal support

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


At present if no expected base is provided, a 0 prefix indicates that the
value is octal, so that 077 is interpreted as octal 77 (hex 3f).

If hex is expected, this is interpreted as hex 77 (hex 4d).

This is a little confusing. We could add a 0t or 0o prefix, but since
octal is so rarely used, drop this feature altogether.

Signed-off-by: Simon Glass <sjg at chromium.org>
Suggested-by: Wolfgang Denk <wd at denx.de>
Fixes: 153d511e369 ("Initial revision")
---

Changes in v2:
- Drop 0o feature and octal support

 doc/usage/cmdline.rst              |  9 ---------
 include/vsprintf.h                 | 25 +++++++++++++++++++------
 lib/strto.c                        | 13 +++++--------
 test/py/tests/test_hush_if_test.py | 11 -----------
 test/str_ut.c                      | 10 ----------
 5 files changed, 24 insertions(+), 44 deletions(-)

diff --git a/doc/usage/cmdline.rst b/doc/usage/cmdline.rst
index ff0f8b77f1c..0184c8f9676 100644
--- a/doc/usage/cmdline.rst
+++ b/doc/usage/cmdline.rst
@@ -84,15 +84,6 @@ Some commands use decimal where it is more natural::
   => i2c speed 100000
   Setting bus speed to 100000 Hz
 
-In some cases the default is decimal but it is possible to use octal if that is
-useful::
-
-  pmic dev pmic at 41
-  dev: 1 @ pmic at 41
-  => pmic write 2 0177
-  => pmic read 2
-  0x02: 0x00007f
-
 It is possible to use a `0x` prefix to use a hex value if that is more
 convenient::
 
diff --git a/include/vsprintf.h b/include/vsprintf.h
index 1ff9562c41b..85879e7a9e0 100644
--- a/include/vsprintf.h
+++ b/include/vsprintf.h
@@ -26,9 +26,15 @@
  * If found, the base is set to hex (16). Similarly a decimal prefix (e.g. 0n12)
  * causes the base to be set to decimal (10).
  *
- * If @base is 0:
- *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
- *    - otherwise the base defaults to decimal (10).
+ * Note that the '0' prefix is not supported for octal. So when @base is 10,
+ * "0123" is interpreted as decimal 123, not octal. This is to avoid confusion
+ * with hex values which can start with 0.
+ *
+ * It would be possible to add a 0o prefix for octal, but this is not
+ * implemented since it is rarely used. See the 'setexpr' command for more
+ * options.
+ *
+ * If @base is 0 it defaults to decimal (10).
  */
 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
 
@@ -80,9 +86,16 @@ unsigned long dectoul(const char *cp, char **endp);
  * If found, the base is set to hex (16). Similarly a decimal prefix (e.g. 0n12)
  * causes the base to be set to decimal (10).
  *
- * If @base is 0:
- *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
- *    - otherwise the base defaults to decimal (10).
+ * Note that the '0' prefix is not supported for octal. So when @base is 10,
+ * "0123" is interpreted as decimal 123, not octal. This is to avoid confusion
+ * with hex values which can start with 0.
+ *
+ * It would be possible to add a 0o prefix for octal, but this is not
+ * implemented since it is rarely used. See the 'setexpr' command for more
+ * options.
+ *
+ *
+ * If @base is 0 it defaults to decimal (10).
  *
  * Copied this function from Linux 2.6.38 commit ID:
  * 521cb40b0c44418a4fd36dc633f575813d59a43d
diff --git a/lib/strto.c b/lib/strto.c
index 0fdc3d68b90..2168d717b25 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -20,16 +20,13 @@ static const char *_parse_integer_fixup_radix(const char *s, uint *basep)
 	if (s[0] == '0') {
 		int ch = tolower(s[1]);
 
-		if (ch == 'x') {
+		s += 2;
+		if (ch == 'x')
 			*basep = 16;
-			s += 2;
-		} else if (ch == 'n') {
+		else if (ch == 'n')
 			*basep = 10;
-			s += 2;
-		} else if (!*basep) {
-			/* Only select octal if we don't have a base */
-			*basep = 8;
-		}
+		else
+			s -= 2;  /* odd, nothing found */
 	}
 
 	/* Use decimal by default */
diff --git a/test/py/tests/test_hush_if_test.py b/test/py/tests/test_hush_if_test.py
index d117921a6ac..b7b7785863f 100644
--- a/test/py/tests/test_hush_if_test.py
+++ b/test/py/tests/test_hush_if_test.py
@@ -56,14 +56,6 @@ subtests = (
     ('test 123 -ge 123', True),
     ('test 123 -ge 456', False),
 
-    # Octal tests
-
-    ('test 010 -eq 010', True),
-    ('test 010 -eq 011', False),
-
-    ('test 010 -ne 011', True),
-    ('test 010 -ne 010', False),
-
     # Hexadecimal tests
 
     ('test 0x2000000 -gt 0x2000001', False),
@@ -72,10 +64,7 @@ subtests = (
 
     # Mixed tests
 
-    ('test 010 -eq 10', False),
-    ('test 010 -ne 10', True),
     ('test 0xa -eq 10', True),
-    ('test 0xa -eq 012', True),
 
     ('test 2000000 -gt 0x1ffffff', False),
     ('test 0x2000000 -gt 1ffffff', True),
diff --git a/test/str_ut.c b/test/str_ut.c
index c3b797b027a..26fc0864dec 100644
--- a/test/str_ut.c
+++ b/test/str_ut.c
@@ -17,8 +17,6 @@ static const char str2[] = "1099abNo, don't bother apologising.";
 static const char str3[] = "0xbI'm sorry you're alive.";
 static const char str4[] = "1234567890123 I lost closer friends";
 static const char str5[] = "0x9876543210the last time I was deloused";
-static const char str6[] = "0778octal is seldom used";
-static const char str7[] = "707it is a piece of computing history";
 
 /* Declare a new str test */
 #define STR_TEST(_name, _flags)		UNIT_TEST(_name, _flags, str_test)
@@ -91,10 +89,6 @@ static int str_simple_strtoul(struct unit_test_state *uts)
 		ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper));
 		ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper));
 
-		/* Octal */
-		ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper));
-		ut_assertok(run_strtoul(uts, str7, 8, 0x1c7, 3, upper));
-
 		/* Invalid string */
 		ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
 
@@ -152,10 +146,6 @@ static int str_simple_strtoull(struct unit_test_state *uts)
 		ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper));
 		ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper));
 
-		/* Octal */
-		ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper));
-		ut_assertok(run_strtoull(uts, str7, 8, 0x1c7, 3, upper));
-
 		/* Large values */
 		ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13,
 					 upper));
-- 
2.32.0.432.gabb21c7263-goog



More information about the U-Boot mailing list