From 5f4d8a396bde29a21775cc0403c64a1d7c2b16c1 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 21 Mar 2025 23:13:50 +0100 Subject: [PATCH] man/man3/strtol.3: CAVEATS: Clarify how to perform range checks Reported-by: Bruno Haible Co-authored-by: Bruno Haible Signed-off-by: Bruno Haible Signed-off-by: Alejandro Colomar --- man/man3/strtol.3 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/man/man3/strtol.3 b/man/man3/strtol.3 index f9c9af4bc..2ae48119b 100644 --- a/man/man3/strtol.3 +++ b/man/man3/strtol.3 @@ -192,6 +192,7 @@ POSIX.1-2001, C89, SVr4, 4.3BSD. .BR strtoll () POSIX.1-2001, C99. .SH CAVEATS +.SS Range checks Since .BR strtol () can legitimately return 0, @@ -210,6 +211,19 @@ and then determine if an error occurred by checking whether .I errno == ERANGE after the call. .P +.in +4n +.EX +errno = 0; +n = strtol(s, &end, base); +if (end == s) + goto no_number; +if ((errno == ERANGE && n == LONG_MIN) || n < min) + goto too_low; +if ((errno == ERANGE && n == LONG_MAX) || n > max) + goto too_high; +.EE +.in +.SS base If the .I base needs to be tested, -- 2.47.2