Re: sscanf/strtoul: parse integers robustly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Junio C Hamano <junkio@xxxxxxx> wrote:

> Jim Meyering <jim@xxxxxxxxxxxx> writes:
>
>> diff --git a/git-compat-util.h b/git-compat-util.h
>> index 139fc19..5f6a281 100644
>> --- a/git-compat-util.h
>> +++ b/git-compat-util.h
>> @@ -301,4 +301,17 @@ static inline int prefixcmp(const char *str, const char *prefix)
>>  	return strncmp(str, prefix, strlen(prefix));
>>  }
>>
>> +static inline int strtoul_ui(char const *s, int base, unsigned int *result)
>> +{
>> +	unsigned long ul;
>> +	char *p;
>> +
>> +	errno = 0;
>> +	ul = strtoul(s, &p, base);
>> +	if (errno || *p || p == s || (unsigned int) ul != ul)
>> +		return -1;
>> +	*result = ul;
>> +	return 0;
>> +}
>> +
>>  #endif
>
> War on sscanf is fine, but I wonder if this is small enough to
> be a good candidate for inlining.

I don't care if it is actually inlined.
I used "inline" because this function seems small enough that
duplicating its code won't hurt, and because then I didn't need
to bother with a separate prototype.  On the size front, it looks
no larger than most of the other inline functions in that file.
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]