From: Alejandro Colomar Date: Fri, 21 Mar 2025 22:13:50 +0000 (+0100) Subject: man/man3/strtol.3: CAVEATS: Clarify how to perform range checks X-Git-Tag: man-pages-6.14~45 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f4d8a396bde29a21775cc0403c64a1d7c2b16c1;p=thirdparty%2Fman-pages.git 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 --- 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,