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

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

From
Max Kirillov <max@max630.net>
Date
Sep 13, 2015, 22:00 UTC
Message-ID
<1442181636-27821-1-git-send-email-max@max630.net>

If s == "-1" and CPU is i386, then none of the checks is triggered, including the last "(unsigned int) ul != ul", because ul == 2**32 - 1, which fits into "unsigned int".

Fix it by changing the last check to trigger earlier, as soon as it becomes bigger than INT_MAX.

Signed-off-by: Max Kirillov <max@max630.net>
---
This caused failure of "%(contents:lines=-1)` should fail" case from
t6302-for-each-ref-filter.sh for me in pu. Don't know why nobody has noticed
it. It did not trigger errno, instead wrapping the value. I have libc6 2.13
(debian wheezy)

Still can be fooled with carefully chosen negative input. For i386 it's between INT_MIN and something like -UINT_MIN

Adding people from the commit which uses the function.
 git-compat-util.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index f649e81..1c0229b 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -815,7 +815,7 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)
 
 	errno = 0;
 	ul = strtoul(s, &p, base);
-	if (errno || *p || p == s || (unsigned int) ul != ul)
+	if (errno || *p || p == s || ul > (unsigned long) INT_MAX)
 		return -1;
 	*result = ul;
 	return 0;
-- 
2.3.4.2801.g3d0809b
Next: Matthieu Moy
Message 1 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.