git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marc Branchaud <marcnarc@xiplink.com>
To: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>
Cc: gitster@pobox.com, git@vger.kernel.org, max@max630.net
Subject: Re: [PATCH] strtoul_ui: reject negative values
Date: Thu, 17 Sep 2015 12:12:11 -0400	[thread overview]
Message-ID: <55FAE65B.1060204@xiplink.com> (raw)
In-Reply-To: <vpqr3lxoy9t.fsf@anie.imag.fr>

On 15-09-17 11:34 AM, Matthieu Moy wrote:
> Marc Branchaud <marcnarc@xiplink.com> writes:
> 
>>> --- 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;
>>
>> I think this is broken, in that it doesn't match strtoul's normal behaviour,
>> for strings like "1234-5678", no?
> 
> The goal here is just to read a positive integer value. Rejecting
> "1234-5678" is indeed a good thing. We already rejected it before my
> patch by checking for p (AKA endptr for strtoul), as you noted below.
> 
>> The test also doesn't work if the string has leading whitespace ("
>> -5").
> 
> Why? It rejects any string that contain the character '-', regardless of
> trailing spaces.

Right, sorry.

>>>  	ul = strtoul(s, &p, base);
>>>  	if (errno || *p || p == s || (unsigned int) ul != ul)
>>>  		return -1;
>>
>> Hmm, but we check *p here, so IIUC it's an error if the string has any
>> trailing non-digits.  Weird.
> 
> strtoul_ui is more defensive than strtoul, by design.

Fair enough, just not what I expected from a function with that name.

		M.

  reply	other threads:[~2015-09-17 16:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-17 14:37 [PATCH] strtoul_ui: reject negative values Matthieu Moy
2015-09-17 15:17 ` Marc Branchaud
2015-09-17 15:34   ` Matthieu Moy
2015-09-17 16:12     ` Marc Branchaud [this message]
2015-09-17 16:18 ` Junio C Hamano
2015-09-17 16:28   ` [PATCH v2] " Matthieu Moy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55FAE65B.1060204@xiplink.com \
    --to=marcnarc@xiplink.com \
    --cc=Matthieu.Moy@grenoble-inp.fr \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=max@max630.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).