[PATCH v2 0/2] strto[u]l(3) BUGS and CAVEATS

Alejandro Colomar
2025-03-21T22:18:16Z

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

Hi Bruno,

This time, I've added a mention to white space. I've decided to put
both bugs in the same commit.

And I've added a commit clarifying how to do range checks correctly.

Have a lovely night!
Alex

Alejandro Colomar (2):
man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not
rejected
man/man3/strtol.3: CAVEATS: Clarify how to perform range checks

man/man3/strtol.3 | 20 ++++++++++++++++++++
man/man3/strtoul.3 | 19 ++++++++++++++-----
2 files changed, 34 insertions(+), 5 deletions(-)

Range-diff against v1:
1: 939641570 ! 1: 8faa6a809 man/man3/strtoul.3: BUGS: Signed numbers are not rejected
@@ Metadata
Author: Alejandro Colomar <alx@kernel.org>

## Commit message ##
- man/man3/strtoul.3: BUGS: Signed numbers are not rejected
+ man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected

Reported-by: Bruno Haible <bruno@clisp.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>

+ ## man/man3/strtol.3 ##
+@@ man/man3/strtol.3: .SH CAVEATS
+ goto unsupported_base;
+ .EE
+ .in
++.SH BUGS
++.SS White space
++These functions silently accept leading white space.
++One should call
++.BR isspace (3)
++before
++.BR strtol ()
++to reject white space.
+ .SH EXAMPLES
+ The program shown below demonstrates the use of
+ .BR strtol ().
+
## man/man3/strtoul.3 ##
@@ man/man3/strtoul.3: .SH CAVEATS
and then determine if an error occurred by checking whether
@@ man/man3/strtoul.3: .SH CAVEATS
-Negative values are considered valid input and are
-silently converted to the equivalent
-.I "unsigned long"
+-value.
+.SH BUGS
+.SS Signed numbers
+Negative values
+are considered valid input and
-+are silently converted to the equivalent
-+.I unsigned long
- value.
-+.P
-+Users should reject signed numbers
-+with a wrapper function.
-+.IP
-+.EX
-+unsigned long
-+strtoul_u(const char *restrict s, char **restrict endp, int base)
-+{
-+ while (isspace((unsigned char) *s))
-+ s++;
-+ if (!isxdigit((unsigned char) *s))
-+ return 0;
-+\&
-+ return strtoul(s, endp, base);
-+}
-+.EE
++are silently converted to
++.IR "\%unsigned\ long" .
++.SS White space
++These functions silently accept leading whitespace.
++.SS isxdigit(3)
++One should call
++.BR isxdigit (3)
++before
++.BR strtoul ()
++to reject white space and/or a sign.
.SH EXAMPLES
See the example on the
.BR strtol (3)
-: --------- > 2: b5244e62c man/man3/strtol.3: CAVEATS: Clarify how to perform range checks

base-commit: e921861a3d30cfc5f9263747a4e64a68e488288c
--
2.47.2