git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] strtoul_ui: actually report error in case of negative input

From
Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Date
Sep 16, 2015, 06:08 UTC
Message-ID
<vpq1tdy99qs.fsf@anie.imag.fr>
In-Reply-To
<20150916042037.GB7806@wheezy.local>
Max Kirillov <max@max630.net> writes:
Show 20 quoted lines
> On Tue, Sep 15, 2015 at 08:50:03AM +0200, Matthieu Moy wrote:
>> --- a/git-compat-util.h
>> +++ b/git-compat-util.h
>> @@ -814,6 +814,9 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)
>>         char *p;
>>  
>>         errno = 0;
>> +       /* negative values would be accepted by strtoul */
>> +       if (strchr(s, '-'))
>> +               return -1;
>>         ul = strtoul(s, &p, base);
>>         if (errno || *p || p == s || (unsigned int) ul != ul)
>>                 return -1;
>> 
>> What do you think?
>
> Explicit rejection of '-' is of course useful addition.
>
> I still find "(unsigned int) ul != ul" bad. As far as I
> understand it makes no sense for i386.

Nothing would make sense here for i386: there's no case where you want to reject anything on this architecture. Well, you may have expected strtoul to reject big numbers, but it did not and it's too late.

> And even for 64-bit it's too obscure. In form of "(ul & 0xffffffffL)
> == 0" it would be more clear.

I disagree. "(unsigned int) ul != ul" reads immediately as "if casting ul to unsigned int changes its value", regardless of sizeof(int). This is exactly what the check is doing.

> Or just make explicit comparison with intended limit, like I did.

What you really want is to compare with UINT_MAX which would not make sense on 32 bits architectures.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Previous: Max Kirillov
Message 7 of 7 in “strtoul_ui: actually report error in case of negative input”
  1. strtoul_ui: actually report error in case of negative inputMax Kirillov, Sep 13, 2015
  2. Matthieu MoySep 14, 2015
  3. Max KirillovSep 14, 2015
  4. Matthieu MoySep 15, 2015
  5. Junio C HamanoSep 16, 2015
  6. Max KirillovSep 16, 2015
  7. Matthieu MoySep 16, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.