Re: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected

Alejandro Colomar
2025-03-23T18:55:59Z

[-- Attachment #1: Type: text/plain, Size: 4865 bytes --]

Hi Bruno,

On Sun, Mar 23, 2025 at 04:52:15PM +0100, Bruno Haible wrote:
> * strtol.3 and strtoul.3 mention
> "If base is zero or 16, the string may then include a "0x" or "0X" prefix,
> and the number will be read in base 16"
> They should also mention:
> "If base is zero or 2, the string may then include a "0b" or "0B" prefix,
> and the number will be read in base 2"
> References:
> - ISO C 23 § 7.24.1.7.(3)
> - https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=64924422a99690d147a166b4de3103f3bf3eaf6c

Here's the proposed fix:

<https://www.alejandro-colomar.es/src/alx/linux/man-pages/man-pages.git/commit/?h=contrib&id=4f35fc20ec258842b713da6b0e7d06fd59b70abe>

alx@devuan:~/src/linux/man-pages/man-pages/contrib$ git show
commit 4f35fc20ec258842b713da6b0e7d06fd59b70abe (HEAD -> contrib, alx/contrib)
Author: Alejandro Colomar <alx@kernel.org>
Date: Sun Mar 23 19:47:33 2025 +0100

man/man3/strto[u]l.3: C23 added "0b" and "0B"

Link: <https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=64924422a99690d147a166b4de3103f3bf3eaf6c>
Reported-by: Bruno Haible <bruno@clisp.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>

diff --git a/man/man3/strtol.3 b/man/man3/strtol.3
index 2ae48119b..9c8791b07 100644
--- a/man/man3/strtol.3
+++ b/man/man3/strtol.3
@@ -53,8 +53,13 @@ .SH DESCRIPTION
If
.I base
is zero or 16, the string may then include a
-"0x" or "0X" prefix, and the number will be read in base 16; otherwise, a
-zero
+"0x" or "0X" prefix, and the number will be read in base 16;
+if
+.I base
+is zero or 2, the string may then include a
+"0b" or "0B" prefix, and the number will be read in base 2;
+otherwise,
+a zero
.I base
is taken as 10 (decimal) unless the next character
is \[aq]0\[aq], in which case it is taken as 8 (octal).
diff --git a/man/man3/strtoul.3 b/man/man3/strtoul.3
index c6a2bb9be..4e85f7a30 100644
--- a/man/man3/strtoul.3
+++ b/man/man3/strtoul.3
@@ -59,8 +59,13 @@ .SH DESCRIPTION
If
.I base
is zero or 16, the string may then include a
-"0x" or "0X" prefix, and the number will be read in base 16; otherwise, a
-zero
+"0x" or "0X" prefix, and the number will be read in base 16;
+if
+.I base
+is zero or 2, the string may then include a
+"0b" or "0B" prefix, and the number will be read in base 2;
+otherwise,
+a zero
.I base
is taken as 10 (decimal) unless the next character
is \[aq]0\[aq], in which case it is taken as 8 (octal).
@@ -182,7 +187,7 @@ .SH VERSIONS
or to
.BR strtoul ().
.SH STANDARDS
-C11, POSIX.1-2008.
+C23, POSIX.1-2008.
.SH HISTORY
.TP
.BR strtoul ()
@@ -190,6 +195,10 @@ .SH HISTORY
.TP
.BR strtoull ()
POSIX.1-2001, C99.
+.TP
+"0b", "0B"
+C23.
+glibc 2.38.
.SH CAVEATS
Since
.BR strtoul ()

And here's a diff of the rendered pages:

alx@devuan:~/src/linux/man-pages/man-pages/contrib$ diffman-git HEAD
--- HEAD^:man/man3/strtol.3
+++ HEAD:man/man3/strtol.3
@@ -32,7 +32,9 @@
space (as determined by isspace(3)) followed by a single
optional '+' or '-' sign. If base is zero or 16, the
string may then include a "0x" or "0X" prefix, and the
- number will be read in base 16; otherwise, a zero base is
+ number will be read in base 16; if base is zero or 2, the
+ string may then include a "0b" or "0B" prefix, and the
+ number will be read in base 2; otherwise, a zero base is
taken as 10 (decimal) unless the next character is '0', in
which case it is taken as 8 (octal).

--- HEAD^:man/man3/strtoul.3
+++ HEAD:man/man3/strtoul.3
@@ -32,7 +32,9 @@
space (as determined by isspace(3)) followed by a single
optional '+' or '-' sign. If base is zero or 16, the
string may then include a "0x" or "0X" prefix, and the
- number will be read in base 16; otherwise, a zero base is
+ number will be read in base 16; if base is zero or 2, the
+ string may then include a "0b" or "0B" prefix, and the
+ number will be read in base 2; otherwise, a zero base is
taken as 10 (decimal) unless the next character is '0', in
which case it is taken as 8 (octal).

@@ -100,7 +102,7 @@
lent to strtoull() or to strtoul().

STANDARDS
- C11, POSIX.1‐2008.
+ C23, POSIX.1‐2008.

HISTORY
strtoul()
@@ -109,6 +111,9 @@
strtoull()
POSIX.1‐2001, C99.

+ "0b", "0B"
+ C23. glibc 2.38.
+
CAVEATS
Since strtoul() can legitimately return 0 or ULONG_MAX
(ULLONG_MAX for strtoull()) on both success and failure,

Have a lovely night!
Alex

--
<https://www.alejandro-colomar.es/>