Re: [PATCH v4 1/2] man/man3/strto[u]l.3: BUGS: Signed numbers and white space are not rejected

Bruno Haible
2025-03-23T15:52:17Z

Alejandro Colomar wrote:
> 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...

> <https://www.alejandro-colomar.es/src/alx/linux/man-pages/man-pages.git/commit/?h=contrib&id=cbcf76d19f864da8c54e41b600ff5661b195b58e>

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