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

Alejandro Colomar
2025-03-23T00:30:39Z

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

Reported-by: Bruno Haible <bruno@clisp.org>
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