man/man3/strtoul.3: BUGS: Signed numbers are not rejected
Alejandro Colomar
maintainerPATCH 1/1 man/man3/strtoul.3: BUGS: Signed numbers are not rejected
---
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(-)
man/man3/strtoul.3 +22 -4
@@ -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
Signature · 1 line
2.47.2
Quoted 2 lines · Alejandro Colomar
This is one of the patches I will apply after your report. Please review.
Looks good to me, except for one line in the sample:
man/man3/strtoul.3 .SH CAVEATS · line 224
Quoted 1 line
man/man3/strtoul.3 +1
-0+ if (!isxdigit((unsigned char) *s))
For a general base, this should be
if (!isalnum((unsigned char) *s))
For base <= 16, it can be simplified to
if (!isxdigit((unsigned char) *s))
For base <= 10, it can be simplified to
if (!isdigit((unsigned char) *s))
Bruno
Alejandro Colomar
maintainerHi,
Quoted 11 lines · Bruno Haible
Quoted 2 lines · Alejandro Colomar
This is one of the patches I will apply after your report. Please review.
Looks good to me, except for one line in the sample:
man/man3/strtoul.3 .SH CAVEATS · line 224
Quoted 1 line
man/man3/strtoul.3 +1
-0+ if (!isxdigit((unsigned char) *s))For a general base, this should be
if (!isalnum((unsigned char) *s))
Hmmm, thanks! I fogot about base 29. :)
I'll take that for v3.
Quoted 8 lines
For base <= 16, it can be simplified to
if (!isxdigit((unsigned char) *s))
For base <= 10, it can be simplified to
if (!isdigit((unsigned char) *s))
Yep, although I won't mention that. Since isalnum(3) works for all, I'll say that.
Cheers,
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>
Alejandro Colomar
maintainerv2 cover strto[u]l(3) BUGS and CAVEATS
In reply to Alejandro ColomarHi Bruno,
This time, I've added a mention to white space. I've decided to put both bugs in the same commit.
And I've added a commit clarifying how to do range checks correctly.
Have a lovely night!
Alex
Alejandro Colomar (2):
man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not
rejected
man/man3/strtol.3: CAVEATS: Clarify how to perform range checks
man/man3/strtol.3 | 20 ++++++++++++++++++++ man/man3/strtoul.3 | 19 ++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-)
Range-diff against v1:
1: 939641570 ! 1: 8faa6a809 man/man3/strtoul.3: BUGS: Signed numbers are not rejected
@@ Metadata
Author: Alejandro Colomar <[email protected]>
## Commit message ##
- man/man3/strtoul.3: BUGS: Signed numbers are not rejected
+ man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
+ ## man/man3/strtol.3 ##
+@@ man/man3/strtol.3: .SH CAVEATS
+ goto unsupported_base;
+ .EE
+ .in
++.SH BUGS
++.SS White space
++These functions silently accept leading white space.
++One should call
++.BR isspace (3)
++before
++.BR strtol ()
++to reject white space.
+ .SH EXAMPLES
+ The program shown below demonstrates the use of
+ .BR strtol ().
+
## man/man3/strtoul.3 ##
@@ man/man3/strtoul.3: .SH CAVEATS
and then determine if an error occurred by checking whether
@@ man/man3/strtoul.3: .SH CAVEATS
-Negative values are considered valid input and are
-silently converted to the equivalent
-.I "unsigned long"
+-value.
+.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
++are silently converted to
++.IR "\%unsigned\ long" .
++.SS White space
++These functions silently accept leading whitespace.
++.SS isxdigit(3)
++One should call
++.BR isxdigit (3)
++before
++.BR strtoul ()
++to reject white space and/or a sign.
.SH EXAMPLES
See the example on the
.BR strtol (3)
-: --------- > 2: b5244e62c man/man3/strtol.3: CAVEATS: Clarify how to perform range checks
base-commit: e921861a3d30cfc5f9263747a4e64a68e488288c
Signature · 1 line
2.47.2
Alejandro Colomar
maintainerPATCH v2 1/2 man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
--- man/man3/strtol.3 | 8 ++++++++ man/man3/strtoul.3 | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-)
man/man3/strtol.3 +8 -0
@@ -224,6 +224,14 @@ .SH CAVEATS goto unsupported_base; .EE .in +.SH BUGS +.SS White space +These functions silently accept leading white space. +One should call +.BR isspace (3) +before +.BR strtol () +to reject white space. .SH EXAMPLES The program shown below demonstrates the use of .BR strtol ().
man/man3/strtoul.3 +14 -5
@@ -204,11 +204,20 @@ .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" -value. +.SH BUGS +.SS Signed numbers +Negative values +are considered valid input and +are silently converted to +.IR "\%unsigned\ long" . +.SS White space +These functions silently accept leading whitespace. +.SS isxdigit(3) +One should call +.BR isxdigit (3) +before +.BR strtoul () +to reject white space and/or a sign. .SH EXAMPLES See the example on the .BR strtol (3)
Signature · 1 line
2.47.2
Alejandro Colomar
maintainerPATCH v2 2/2 man/man3/strtol.3: CAVEATS: Clarify how to perform range checks
In reply to Alejandro Colomar--- man/man3/strtol.3 | 12 ++++++++++++ 1 file changed, 12 insertions(+)
man/man3/strtol.3 +12 -0
@@ -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,
Signature · 1 line
2.47.2
Alejandro Colomar
maintainerv3 cover strto[u]l.3: BUGS and CAVEATS
In reply to Alejandro ColomarHi!
Here's v3, suggesting alnum(3) instead of isxdigit(3), and also making the wording slightly more precise, since not all negative numbers are accepted; only some (I don't feel like explaining it with details; it's easier to just say it's a bug to be workarounded). Add a link.
Cheers,
Alex
Alejandro Colomar (2):
man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not
rejected
man/man3/strtol.3: CAVEATS: Clarify how to perform range checks
man/man3/strtol.3 | 20 ++++++++++++++++++++ man/man3/strtoul.3 | 19 ++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-)
Range-diff against v2:
1: 8faa6a809 ! 1: 3c456a1a0 man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
@@ Metadata
## Commit message ##
man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
@@ man/man3/strtoul.3: .SH CAVEATS
-value.
+.SH BUGS
+.SS Signed numbers
-+Negative values
++Some negative values
+are considered valid input and
+are silently converted to
+.IR "\%unsigned\ long" .
+.SS White space
+These functions silently accept leading whitespace.
-+.SS isxdigit(3)
++.SS isalnum(3)
+One should call
-+.BR isxdigit (3)
++.BR isalnum (3)
+before
+.BR strtoul ()
+to reject white space and/or a sign.
2: b5244e62c = 2: 020b468a3 man/man3/strtol.3: CAVEATS: Clarify how to perform range checks
base-commit: e921861a3d30cfc5f9263747a4e64a68e488288c
Signature · 1 line
2.47.2
Alejandro Colomar
maintainerPATCH v3 1/2 man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
--- man/man3/strtol.3 | 8 ++++++++ man/man3/strtoul.3 | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-)
man/man3/strtol.3 +8 -0
@@ -224,6 +224,14 @@ .SH CAVEATS goto unsupported_base; .EE .in +.SH BUGS +.SS White space +These functions silently accept leading white space. +One should call +.BR isspace (3) +before +.BR strtol () +to reject white space. .SH EXAMPLES The program shown below demonstrates the use of .BR strtol ().
man/man3/strtoul.3 +14 -5
@@ -204,11 +204,20 @@ .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" -value. +.SH BUGS +.SS Signed numbers +Some negative values +are considered valid input and +are silently converted to +.IR "\%unsigned\ long" . +.SS White space +These functions silently accept leading whitespace. +.SS isalnum(3) +One should call +.BR isalnum (3) +before +.BR strtoul () +to reject white space and/or a sign. .SH EXAMPLES See the example on the .BR strtol (3)
Signature · 1 line
2.47.2
Re: [PATCH v3 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Thanks for theses BUGS sections, Alejandro.
man/man3/strtol.3 .SH CAVEATS · line 234
Quoted 7 lines
+.SS White space
man/man3/strtol.3 +6
-0+These functions silently accept leading white space. +One should call +.BR isspace (3) +before +.BR strtol () +to reject white space.
Just a question of wording, but it would be more straightforward to read when written like this:
+.SS White space
+These functions silently accept leading white space. +In situations where rejecting white space is desired instead, call +.BR isspace (3)
+before
+.BR strtol ().
(As usual, presenting the "why" and "what" before the "how" is nicer. [1])
man/man3/strtoul.3 .SH CAVEATS · line 220
Quoted 5 lines
man/man3/strtoul.3 +5
-0+One should call +.BR isalnum (3) +before +.BR strtoul () +to reject white space and/or a sign.
Likewise here. It is more straightforward when written like this:
+To reject white space and/or a sign, call
+.BR isalnum (3)
+before
+.BR strtoul ().
Bruno
[1] https://gitlab.com/ghwiki/gnow-how/-/wikis/Documentation/Writing
Alejandro Colomar
maintainerRe: [PATCH v3 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Hi Bruno,
Quoted 20 lines · Bruno Haible
Thanks for theses BUGS sections, Alejandro.
man/man3/strtol.3 .SH CAVEATS · line 234
Quoted 7 lines
+.SS White space
man/man3/strtol.3 +6
-0+These functions silently accept leading white space. +One should call +.BR isspace (3) +before +.BR strtol () +to reject white space.Just a question of wording, but it would be more straightforward to read when written like this:
+.SS White space
man/man3/strtol.3 +5
-0+These functions silently accept leading white space. +In situations where rejecting white space is desired instead, call +.BR isspace (3) +before +.BR strtol ().
Quoted 1 line
(As usual, presenting the "why" and "what" before the "how" is nicer. [1])
LGTM. I'd make it shorter, though. How about this?
+.SS White space
+These functions silently accept leading white space. +To reject white space, call
+.BR isspace (3)
+before
+.BR strtol ().
which BTW makes it more consistent with your proposal from below.
Quoted 14 lines
man/man3/strtoul.3 .SH CAVEATS · line 220
Quoted 6 lines
man/man3/strtoul.3 +5
-0+One should call +.BR isalnum (3) +before +.BR strtoul () +to reject white space and/or a sign.Likewise here. It is more straightforward when written like this:
man/man3/strtoul.3 +4
-0+To reject white space and/or a sign, call +.BR isalnum (3) +before +.BR strtoul ().
Quoted 3 lines
Bruno
[1] https://gitlab.com/ghwiki/gnow-how/-/wikis/Documentation/Writing
Thanks! That's useful. :)
Have a lovely day!
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>
Alejandro Colomar
maintainerPATCH v3 2/2 man/man3/strtol.3: CAVEATS: Clarify how to perform range checks
In reply to Alejandro Colomar--- man/man3/strtol.3 | 12 ++++++++++++ 1 file changed, 12 insertions(+)
man/man3/strtol.3 +12 -0
@@ -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,
Signature · 1 line
2.47.2
Alejandro Colomar
maintainerv4 cover strto[u]l.3: BUGS and CAVEATS
In reply to Alejandro ColomarHi!
Here's v4, with minor wording fixes. Bruno, I've added you as Co-authored-by in patch 1/2. Please sign the patch if you like it.
Cheers,
Alex
Alejandro Colomar (2):
man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not
rejected
man/man3/strtol.3: CAVEATS: Clarify how to perform range checks
man/man3/strtol.3 | 19 +++++++++++++++++++ man/man3/strtoul.3 | 18 +++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-)
Range-diff against v3:
1: 3c456a1a0 ! 1: 4a1de398d man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
@@ Commit message
+ Co-authored-by: Bruno Haible <[email protected]>
## man/man3/strtol.3 ##
@@ man/man3/strtol.3: .SH CAVEATS
+.SH BUGS
+.SS White space
+These functions silently accept leading white space.
-+One should call
++To reject white space, call
+.BR isspace (3)
+before
-+.BR strtol ()
-+to reject white space.
++.BR strtol ().
.SH EXAMPLES
The program shown below demonstrates the use of
.BR strtol ().
@@ man/man3/strtoul.3: .SH CAVEATS
+.SS White space
+These functions silently accept leading whitespace.
+.SS isalnum(3)
-+One should call
++To reject white space and/or a sign, call
+.BR isalnum (3)
+before
-+.BR strtoul ()
-+to reject white space and/or a sign.
++.BR strtoul ().
.SH EXAMPLES
See the example on the
.BR strtol (3)
2: 020b468a3 = 2: 98af3aa6c man/man3/strtol.3: CAVEATS: Clarify how to perform range checks
Signature · 1 line
2.47.2
Alejandro Colomar
maintainerPATCH v4 1/2 man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
--- man/man3/strtol.3 | 7 +++++++ man/man3/strtoul.3 | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-)
man/man3/strtol.3 +7 -0
@@ -224,6 +224,13 @@ .SH CAVEATS goto unsupported_base; .EE .in +.SH BUGS +.SS White space +These functions silently accept leading white space. +To reject white space, call +.BR isspace (3) +before +.BR strtol (). .SH EXAMPLES The program shown below demonstrates the use of .BR strtol ().
man/man3/strtoul.3 +13 -5
@@ -204,11 +204,19 @@ .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" -value. +.SH BUGS +.SS Signed numbers +Some negative values +are considered valid input and +are silently converted to +.IR "\%unsigned\ long" . +.SS White space +These functions silently accept leading whitespace. +.SS isalnum(3) +To reject white space and/or a sign, call +.BR isalnum (3) +before +.BR strtoul (). .SH EXAMPLES See the example on the .BR strtol (3)
Signature · 1 line
2.47.2
Re: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Looks all good to me.
Alejandro Colomar
maintainerRe: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Hi Bruno,
Quoted 3 lines · Bruno Haible
Looks all good to me.
Signed-off-by Bruno Haible <[email protected]>
Thanks! I've applied this patch:
<https://www.alejandro-colomar.es/src/alx/linux/man-pages/man-pages.git/commit/?h=contrib&id=cbcf76d19f864da8c54e41b600ff5661b195b58e>
Is patch 2/2 also good to you?
Have a lovely day!
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>
Re: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Quoted 1 line · Alejandro Colomar
Is patch 2/2 also good to you?
I couldn't tell without looking at the entire page ("groff -Tutf8 -mandoc strtol.3 | less -R"). But now...
Quoted 1 line
you gave me the information where to look at your work-in-progress tree.
Here are my findings on these two pages:
* In strtol.3, the example has three mistakes:
- Missing semicolon at the end of the second line.
- If LONG_MIN < min < LONG_MAX, the condition (errno == ERANGE && n == min) will never be true.
If LONG_MIN < max < LONG_MAX, the condition (errno == ERANGE && n == max) will never be true.
- It does not distinguish success with value 0 from failure due to
no digits. (This matters when min <= 0 <= max.) Since errno is not
guaranteed to be set in this case, the caller needs to look at *endptr.
The example thus becomes:
char *end;
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;* strtol.3 and strtoul.3 mention
"If base is zero or 16, the string may then include a "0x" or "0X" prefix,
and the number will be read in base 16"
They should also mention:
"If base is zero or 2, the string may then include a "0b" or "0B" prefix,
and the number will be read in base 2"
References:
- ISO C 23 § 7.24.1.7.(3)
- https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=64924422a99690d147a166b4de3103f3bf3eaf6c
Bruno
Alejandro Colomar
maintainerRe: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Hi Bruno,
Quoted 9 lines · Bruno Haible
Quoted 1 line · Alejandro Colomar
Is patch 2/2 also good to you?
I couldn't tell without looking at the entire page ("groff -Tutf8 -mandoc strtol.3 | less -R"). But now...
Quoted 1 line
you gave me the information where to look at your work-in-progress tree.
Yup. :)
Quoted 4 lines
Here are my findings on these two pages:
* In strtol.3, the example has three mistakes:
- Missing semicolon at the end of the second line.
Thanks!
Quoted 4 lines
- If
LONG_MIN< min <LONG_MAX, the condition (errno == ERANGE && n == min) will never be true.
IfLONG_MIN< max <LONG_MAX, the condition (errno == ERANGE && n == max) will never be true.
D'oh! I'm too used to strtoi(3bsd) that I make these mistakes when switching to strtol(3).
Quoted 4 lines
- It does not distinguish success with value 0 from failure due to no digits. (This matters when min <= 0 <= max.) Since errno is not guaranteed to be set in this case, the caller needs to look at *endptr. The example thus becomes:
Yeah, I wanted to isolate the range checking from the rest. Maybe I should just put this in that sample code?:
if ((errno == ERANGE && n == LONG_MIN) || n < min)
goto too_low;
if ((errno == ERANGE && n == LONG_MAX) || n > max)
goto too_high;Without having the strtol(3) call at all?
Quoted 10 lines
char *end;
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;On the other hand, I should put this in the EXAMPLES section, to have a full example.
Quoted 7 lines
* strtol.3 and strtoul.3 mention
"If base is zero or 16, the string may then include a "0x" or "0X" prefix,
and the number will be read in base 16"
They should also mention:
"If base is zero or 2, the string may then include a "0b" or "0B" prefix,
and the number will be read in base 2"
Yep, I need to update many pages for C23. I'll do this update for this page now.
Thanks!
Cheers,
Alex
Quoted 9 lines
References:
- ISO C 23 § 7.24.1.7.(3)
- https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=64924422a99690d147a166b4de3103f3bf3eaf6cBruno
Signature · 1 line
<https://www.alejandro-colomar.es/>
Alejandro Colomar
maintainerRe: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Quoted 38 lines · Alejandro Colomar
Hi Bruno,
Quoted 9 lines · Bruno Haible
Quoted 1 line · Alejandro Colomar
Is patch 2/2 also good to you?
I couldn't tell without looking at the entire page ("groff -Tutf8 -mandoc strtol.3 | less -R"). But now...
Quoted 1 line
you gave me the information where to look at your work-in-progress tree.
Yup. :)
Quoted 4 lines
Here are my findings on these two pages:
* In strtol.3, the example has three mistakes:
- Missing semicolon at the end of the second line.Thanks!
Quoted 4 lines
- If
LONG_MIN< min <LONG_MAX, the condition (errno == ERANGE && n == min) will never be true.
IfLONG_MIN< max <LONG_MAX, the condition (errno == ERANGE && n == max) will never be true.D'oh! I'm too used to strtoi(3bsd) that I make these mistakes when switching to strtol(3).
Quoted 4 lines
- It does not distinguish success with value 0 from failure due to no digits. (This matters when min <= 0 <= max.) Since errno is not guaranteed to be set in this case, the caller needs to look at *endptr. The example thus becomes:
Yeah, I wanted to isolate the range checking from the rest. Maybe I should just put this in that sample code?:
Quoted 18 lines
if ((errno == ERANGE && n == LONG_MIN) || n < min) goto too_low; if ((errno == ERANGE && n == LONG_MAX) || n > max) goto too_high;Without having the strtol(3) call at all?
Quoted 10 lines
char *end; 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;
I ended up doing what you said:
Quoted 32 lines
On the other hand, I should put this in the EXAMPLES section, to have a full example.
Quoted 7 lines
* strtol.3 and strtoul.3 mention
"If base is zero or 16, the string may then include a "0x" or "0X" prefix,
and the number will be read in base 16"
They should also mention:
"If base is zero or 2, the string may then include a "0b" or "0B" prefix,
and the number will be read in base 2"Yep, I need to update many pages for C23. I'll do this update for this page now.
Thanks!
Cheers,
AlexQuoted 9 lines
References:
- ISO C 23 § 7.24.1.7.(3)
- https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=64924422a99690d147a166b4de3103f3bf3eaf6cBruno
Signature · 1 line
<https://www.alejandro-colomar.es/>
Signature · 1 line
<https://www.alejandro-colomar.es/>
Re: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Alejandro,
Quoted 3 lines
I ended up doing what you said:
+if ((errno == ERANGE && n == LONG_MIN) || n > max)
Here: LONG_MIN -> LONG_MAX
Alejandro Colomar
maintainerRe: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Quoted 11 lines · Bruno Haible
Alejandro,
Quoted 3 lines
I ended up doing what you said:
+if ((errno == ERANGE && n ==
LONG_MIN) || n > max)Here:
LONG_MIN->LONG_MAXSigned-off-by Bruno Haible <[email protected]>
Thanks! Ameded and pushed.
Cheers,
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>
Alejandro Colomar
maintainerRe: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
In reply to Bruno HaibleHi Bruno,
Quoted 9 lines · Bruno Haible
* strtol.3 and strtoul.3 mention
"If base is zero or 16, the string may then include a "0x" or "0X" prefix,
and the number will be read in base 16"
They should also mention:
"If base is zero or 2, the string may then include a "0b" or "0B" prefix,
and the number will be read in base 2"
References:
- ISO C 23 § 7.24.1.7.(3)
- https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=64924422a99690d147a166b4de3103f3bf3eaf6c
Here's the proposed fix:
alx@devuan:~/src/linux/man-pages/man-pages/contrib$ git show commit 4f35fc20ec258842b713da6b0e7d06fd59b70abe (HEAD -> contrib, alx/contrib) Author: Alejandro Colomar <[email protected]> Date: Sun Mar 23 19:47:33 2025 +0100
man/man3/strto[u]l.3: C23 added "0b" and "0B" Link: <https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=64924422a99690d147a166b4de3103f3bf3eaf6c> Reported-by: Bruno Haible <[email protected]> Signed-off-by: Alejandro Colomar <[email protected]> diff --git a/man/man3/strtol.3 b/man/man3/strtol.3 index 2ae48119b..9c8791b07 100644 --- a/man/man3/strtol.3 +++ b/man/man3/strtol.3 @@ -53,8 +53,13 @@ .SH DESCRIPTION If .I base is zero or 16, the string may then include a -"0x" or "0X" prefix, and the number will be read in base 16; otherwise, a -zero +"0x" or "0X" prefix, and the number will be read in base 16; +if +.I base +is zero or 2, the string may then include a +"0b" or "0B" prefix, and the number will be read in base 2; +otherwise, +a zero .I base is taken as 10 (decimal) unless the next character is \[aq]0\[aq], in which case it is taken as 8 (octal). diff --git a/man/man3/strtoul.3 b/man/man3/strtoul.3 index c6a2bb9be..4e85f7a30 100644 --- a/man/man3/strtoul.3 +++ b/man/man3/strtoul.3 @@ -59,8 +59,13 @@ .SH DESCRIPTION If .I base is zero or 16, the string may then include a -"0x" or "0X" prefix, and the number will be read in base 16; otherwise, a -zero +"0x" or "0X" prefix, and the number will be read in base 16; +if +.I base +is zero or 2, the string may then include a +"0b" or "0B" prefix, and the number will be read in base 2; +otherwise, +a zero .I base is taken as 10 (decimal) unless the next character is \[aq]0\[aq], in which case it is taken as 8 (octal). @@ -182,7 +187,7 @@ .SH VERSIONS or to .BR strtoul (). .SH STANDARDS -C11, POSIX.1-2008. +C23, POSIX.1-2008. .SH HISTORY .TP .BR strtoul () @@ -190,6 +195,10 @@ .SH HISTORY .TP .BR strtoull () POSIX.1-2001, C99. +.TP +"0b", "0B" +C23. +glibc 2.38. .SH CAVEATS Since .BR strtoul ()
And here's a diff of the rendered pages:
alx@devuan:~/src/linux/man-pages/man-pages/contrib$ diffman-git HEAD --- HEAD^:man/man3/strtol.3 +++ HEAD:man/man3/strtol.3 @@ -32,7 +32,9 @@ space (as determined by isspace(3)) followed by a single optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" or "0X" prefix, and the - number will be read in base 16; otherwise, a zero base is + number will be read in base 16; if base is zero or 2, the + string may then include a "0b" or "0B" prefix, and the + number will be read in base 2; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).
--- HEAD^:man/man3/strtoul.3
+++ HEAD:man/man3/strtoul.3
@@ -32,7 +32,9 @@
space (as determined by isspace(3)) followed by a single optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" or "0X" prefix, and the
- number will be read in base 16; otherwise, a zero base is
+ number will be read in base 16; if base is zero or 2, the
+ string may then include a "0b" or "0B" prefix, and the
+ number will be read in base 2; otherwise, a zero base is
taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).
@@ -100,7 +102,7 @@
lent to strtoull() or to strtoul().
STANDARDS
- C11, POSIX.1‐2008.
+ C23, POSIX.1‐2008.
HISTORY
strtoul()
@@ -109,6 +111,9 @@
strtoull()
POSIX.1‐2001, C99.
+ "0b", "0B"
+ C23. glibc 2.38.
+
CAVEATS
Since strtoul() can legitimately return 0 or ULONG_MAX (ULLONG_MAX for strtoull()) on both success and failure,
Have a lovely night!
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>
Re: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Quoted 16 lines · Alejandro Colomar
diff --git a/man/man3/strtoul.3 b/man/man3/strtoul.3 index c6a2bb9be..4e85f7a30 100644 --- a/man/man3/strtoul.3 +++ b/man/man3/strtoul.3 @@ -182,7 +187,7 @@ .SH VERSIONS or to .BR strtoul (). .SH STANDARDS -C11, POSIX.1-2008. +C23, POSIX.1-2008. .SH HISTORY .TP .BR strtoul () @@ -190,6 +195,10 @@ .SH HISTORY .TP .BR strtoull ()
Quoted 8 lines
POSIX.1-2001, C99. +.TP +"0b", "0B" +C23. +glibc 2.38. .SH CAVEATS Since .BR strtoul ()
These same two hunks could also be applied to strtol.3.
Bruno
Alejandro Colomar
maintainerRe: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected
Quoted 26 lines · Bruno Haible
Quoted 16 lines · Alejandro Colomar
diff --git a/man/man3/strtoul.3 b/man/man3/strtoul.3 index c6a2bb9be..4e85f7a30 100644 --- a/man/man3/strtoul.3 +++ b/man/man3/strtoul.3 @@ -182,7 +187,7 @@ .SH VERSIONS or to .BR strtoul (). .SH STANDARDS -C11, POSIX.1-2008. +C23, POSIX.1-2008. .SH HISTORY .TP .BR strtoul () @@ -190,6 +195,10 @@ .SH HISTORY .TP .BR strtoull ()Quoted 8 lines
POSIX.1-2001, C99. +.TP +"0b", "0B" +C23. +glibc 2.38. .SH CAVEATS Since .BR strtoul ()
Quoted 1 line
These same two hunks could also be applied to strtol.3.
Yep, I had forgotten. I've applied them a moment ago. BTW, I also updated the info about POSIX too:
@@ -183,7 +188,7 @@ .SH VERSIONS or to .BR strtol (). .SH STANDARDS -C11, POSIX.1-2008. +C23, POSIX.1-2024. .SH HISTORY .TP .BR strtol () @@ -191,6 +196,11 @@ .SH HISTORY .TP .BR strtoll () POSIX.1-2001, C99. +.TP +"0b", "0B" +C23. +glibc 2.38. +(Not in POSIX.) .SH CAVEATS .SS Range checks Since
Cheers,
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>
Alejandro Colomar
maintainerPATCH v4 2/2 man/man3/strtol.3: CAVEATS: Clarify how to perform range checks
In reply to Alejandro Colomar--- man/man3/strtol.3 | 12 ++++++++++++ 1 file changed, 12 insertions(+)
man/man3/strtol.3 +12 -0
@@ -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,
Signature · 1 line
2.47.2