Re: [PATCH v2 4/5] parse-options: introduce `OPTION_UNSIGNED`
- From
- phillip.wood123@gmail.com <phillip.wood123@gmail.com>
- Date
- Apr 16, 2025, 13:31 UTC
- Message-ID
- <7c684f2b-ca56-4e8b-b141-0c2f6b64a44a@gmail.com>
- In-Reply-To
- <Z_-GKX8uFSNib5rW@pks.im>
On 16/04/2025 11:27, Patrick Steinhardt wrote:
Show 12 quoted lines
> On Tue, Apr 15, 2025 at 04:52:02PM +0100, Phillip Wood wrote: > > Wait, does it? Why would `strtoul()` or any of its variants ever accept > a string prefixed with a "-"? > > If the minus sign was part of the input sequence, the numeric value > calculated from the sequence of digits is negated as if by unary > minus in the result type, which applies unsigned integer wraparound > rules. > > Oh dear... all these integer conversion functions are really a gift that > keeps on giving. Gross.
Yes it's horrible behavior for a function that parses an unsigned integer. We should perhaps think about adding 64 bit versions of strtoui_i() and strtoul_ui() so we have a safer option for parsing large integers but I don't think we need to do that as part of this series.
I'll try and take a look at V3 tomorrow
Phillip
Show 6 quoted lines
>> This patch also needs the fix from patch 2 to detect overflows for >> uintmax_t. > > Yup, will add. > > Patrick