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

Re: sscanf/strtoul: parse integers robustly

From
Jim Meyering <jim@meyering.net>
Date
Apr 11, 2007, 13:52 UTC
Message-ID
<87fy77t3we.fsf@rho.meyering.net>
In-Reply-To
<7v4pnnqr9q.fsf@assigned-by-dhcp.cox.net>
Junio C Hamano <junkio@cox.net> wrote:
Show 27 quoted lines
> Jim Meyering <jim@meyering.net> 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.

Previous: Junio C HamanoNext: Andy Whitcroft
Message 3 of 5 in “sscanf/strtoul: parse integers robustly”
  1. Jim MeyeringApr 9, 2007
  2. Junio C HamanoApr 11, 2007
  3. Jim MeyeringApr 11, 2007
  4. fix up strtoul_ui error handlingAndy Whitcroft, Apr 19, 2007
  5. Junio C HamanoApr 19, 2007

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.