Thread (24 messages) 24 messages, 2 authors, 2025-03-23

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

From: Alejandro Colomar <alx@kernel.org>
Date: 2025-03-21 20:41:58
Subsystem: the rest · Maintainer: Linus Torvalds

Reported-by: Bruno Haible <redacted>
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

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help