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
Max Kirillov <max@max630.net>
Date
Sep 16, 2015, 04:20 UTC
Message-ID
<20150916042037.GB7806@wheezy.local>
In-Reply-To
<vpq4miwfa78.fsf@anie.imag.fr>
On Tue, Sep 15, 2015 at 08:50:03AM +0200, Matthieu Moy wrote:
> I think it would be better to just return a long to avoid needless
> limitations, but changing the argument to "long" would interfer with
> in-flight topics. Not worth the trouble.
Sure.
> 
> One potential issue with your patch is that you're forbidding the
> interval [2^31, 2^32[ which was previously allowed, both on 32 and 64
> bits. I'm not sure whether we have a use for this in the codebase.

As far as I could see it was used only for file modes. Which does not need that big numbers.

Show 18 quoted lines
> This alternative patch is rather ugly to, but I think it is less
> limiting and does not have the "large negative wrapped to positive"
> issue:
> 
> --- 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. And even for 64-bit it's too obscure. In form of "(ul & 0xffffffffL) == 0" it would be more clear. Or just make explicit comparison with intended limit, like I did.

Well, actually I don't have strong preferences as long as "make -C t" does not alarm me with things I did not break. Maybe somebody else will comment more.

-- 
Max
Previous: Junio C HamanoNext: Matthieu Moy
Message 6 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.