[PATCH v1] man/man3/strtoul.3: BUGS: Signed numbers are not rejected

Alejandro Colomar
2025-03-21T20:41:58Z

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

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

Hi Bruno,

This is one of the patches I will apply after your report. Please
review. Thanks!

Cheers,
Alex

man/man3/strtoul.3 | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/man/man3/strtoul.3 b/man/man3/strtoul.3
index 9eb260dae..62b9f65f6 100644
--- a/man/man3/strtoul.3
+++ b/man/man3/strtoul.3
@@ -204,11 +204,29 @@ .SH CAVEATS
and then determine if an error occurred by checking whether
.I errno
has a nonzero value after the call.
-.P
-Negative values are considered valid input and are
-silently converted to the equivalent
-.I "unsigned long"
+.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
.SH EXAMPLES
See the example on the
.BR strtol (3)

Range-diff against v0:
-: --------- > 1: 939641570 man/man3/strtoul.3: BUGS: Signed numbers are not rejected

base-commit: e921861a3d30cfc5f9263747a4e64a68e488288c
--
2.47.2