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

[PATCH 1/5] config: factor out integer parsing from range checks

From
Jeff King <peff@peff.net>
Date
Sep 8, 2013, 08:29 UTC
Message-ID
<20130908082927.GA29554@sigill.intra.peff.net>
In-Reply-To
<20130908082724.GA27184@sigill.intra.peff.net>

When we are parsing integers for config, we use an intmax_t (or uintmax_t) internally, and then check against the size of our result type at the end. We can parameterize the maximum representable value, which will let us re-use the parsing code for a variety of range checks.

Unfortunately, we cannot combine the signed and unsigned parsing functions easily, as we have to rely on the signed and unsigned C types internally.

Signed-off-by: Jeff King <peff@peff.net>
---
The fact that we have two nearly-identical functions next to each other
really bugs me (especially since I'm going to make identical changes to
them later in this series). But I think trying to factor them out into a
single function would end up being even less readable. I'd be happy if
somebody wants to prove me wrong.
 config.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
diff --git a/config.c b/config.c
index 9f9bf0c..ee7c1f8 100644
--- a/config.c
+++ b/config.c
@@ -468,7 +468,7 @@ static int parse_unit_factor(const char *end, uintmax_t *val)
 	return 0;
 }
 
-static int git_parse_long(const char *value, long *ret)
+static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
 {
 	if (value && *value) {
 		char *end;
@@ -484,8 +484,7 @@ static int git_parse_long(const char *value, long *ret)
 			return 0;
 		uval = abs(val);
 		uval *= factor;
-		if ((uval > maximum_signed_value_of_type(long)) ||
-		    (abs(val) > uval))
+		if (uval > max || abs(val) > uval)
 			return 0;
 		val *= factor;
 		*ret = val;
@@ -494,7 +493,7 @@ static int git_parse_long(const char *value, long *ret)
 	return 0;
 }
 
-int git_parse_ulong(const char *value, unsigned long *ret)
+int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
 {
 	if (value && *value) {
 		char *end;
@@ -508,8 +507,7 @@ int git_parse_ulong(const char *value, unsigned long *ret)
 		oldval = val;
 		if (!parse_unit_factor(end, &val))
 			return 0;
-		if ((val > maximum_unsigned_value_of_type(long)) ||
-		    (oldval > val))
+		if (val > max || oldval > val)
 			return 0;
 		*ret = val;
 		return 1;
@@ -517,6 +515,24 @@ int git_parse_ulong(const char *value, unsigned long *ret)
 	return 0;
 }
 
+static int git_parse_long(const char *value, long *ret)
+{
+	intmax_t tmp;
+	if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(long)))
+		return 0;
+	*ret = tmp;
+	return 1;
+}
+
+int git_parse_ulong(const char *value, unsigned long *ret)
+{
+	uintmax_t tmp;
+	if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
+		return 0;
+	*ret = tmp;
+	return 1;
+}
+
 static void die_bad_config(const char *name)
 {
 	if (cf && cf->name)
-- 
1.8.4.2.g87d4a77
Previous: Jeff KingNext: Jeff King
Message 18 of 25 in “git-config and large integers”
  1. 0/2 git-config and large integersJeff King, Aug 20, 2013
  2. 1/2 config: properly range-check integer valuesJeff King, Aug 20, 2013
  3. Jonathan NiederAug 20, 2013
  4. Jeff KingAug 21, 2013
  5. Stefan BellerAug 20, 2013
  6. Jeff KingAug 20, 2013
  7. 2/2 teach git-config to output large integersJeff King, Aug 20, 2013
  8. Jonathan NiederAug 20, 2013
  9. Jeff KingAug 21, 2013
  10. Jonathan NiederAug 21, 2013
  11. Jeff KingAug 21, 2013
  12. Jonathan NiederAug 21, 2013
  13. Junio C HamanoAug 20, 2013
  14. Junio C HamanoAug 20, 2013
  15. Jeff KingAug 21, 2013
  16. Jeff KingAug 21, 2013
  17. 0/5 git-config and large integersJeff King, Sep 8, 2013
  18. 1/5 config: factor out integer parsing from range checksJeff King, Sep 8, 2013
  19. 2/5 config: properly range-check integer valuesJeff King, Sep 8, 2013
  20. 3/5 config: set errno in numeric git_parse_* functionsJeff King, Sep 8, 2013
  21. Eric SunshineSep 9, 2013
  22. Jeff KingSep 9, 2013
  23. 4/5 config: make numeric parsing errors more clearJeff King, Sep 8, 2013
  24. 5/5 git-config: always treat --int as 64-bit internallyJeff King, Sep 8, 2013
  25. Junio C HamanoSep 9, 2013

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.