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

Alejandro Colomar
2025-03-23T16:55:50Z

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

Hi Bruno,

On Sun, Mar 23, 2025 at 04:52:15PM +0100, Bruno Haible wrote:
> 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.

Yup. :)

> 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!

> - 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.

D'oh! I'm too used to strtoi(3bsd) that I make these mistakes when
switching to strtol(3).

> - 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?

>
> 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.

>
> * 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

> References:
> - ISO C 23 § 7.24.1.7.(3)
> - https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=64924422a99690d147a166b4de3103f3bf3eaf6c
>
> Bruno
>
>
>
>

--
<https://www.alejandro-colomar.es/>