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

[PATCH v2 2/2] man/man3/strtol.3: CAVEATS: Clarify how to perform range checks

From: Alejandro Colomar <alx@kernel.org>
Date: 2025-03-21 22:18:45
Subsystem: the rest · Maintainer: Linus Torvalds

Reported-by: Bruno Haible <redacted>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 man/man3/strtol.3 | 12 ++++++++++++
 1 file changed, 12 insertions(+)
diff --git a/man/man3/strtol.3 b/man/man3/strtol.3
index 03047f10a..973d6a78b 100644
--- a/man/man3/strtol.3
+++ b/man/man3/strtol.3
@@ -192,6 +192,7 @@ .SH HISTORY
 .BR strtoll ()
 POSIX.1-2001, C99.
 .SH CAVEATS
+.SS Range checks
 Since
 .BR strtol ()
 can legitimately return 0,
@@ -210,6 +211,17 @@ .SH CAVEATS
 .I errno == ERANGE
 after the call.
 .P
+.in +4n
+.EX
+errno = 0;
+n = strtol(s, NULL, base)
+if ((errno == ERANGE && n == min) || n < min)
+	goto too_low;
+if ((errno == ERANGE && n == max) || n > max)
+	goto too_high;
+.EE
+.in
+.SS base
 If the
 .I base
 needs to be tested,
-- 
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