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

[PATCH v5 2/4] Change GIT_ALLOC_LIMIT check to use git_parse_ulong()

From
Steffen Prohaska <prohaska@zib.de>
Date
Aug 24, 2014, 16:07 UTC
Message-ID
<1408896466-23149-3-git-send-email-prohaska@zib.de>
In-Reply-To
<1408896466-23149-1-git-send-email-prohaska@zib.de>

GIT_ALLOC_LIMIT limits xmalloc()'s size, which is of type size_t. Better use git_parse_ulong() to parse the environment variable, so that the postfixes 'k', 'm', and 'g' can be used; and use size_t to store the limit for consistency. The change to size_t has no direct practical impact, because we use GIT_ALLOC_LIMIT to test small sizes.

The cast of size in the call to die() is changed to uintmax_t to match the format string PRIuMAX.

Signed-off-by: Steffen Prohaska <prohaska@zib.de>
---
 t/t1050-large.sh |  2 +-
 wrapper.c        | 16 ++++++++++------
 2 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/t/t1050-large.sh b/t/t1050-large.sh
index aea4936..e7657ab 100755
--- a/t/t1050-large.sh
+++ b/t/t1050-large.sh
@@ -13,7 +13,7 @@ test_expect_success setup '
 	echo X | dd of=large2 bs=1k seek=2000 &&
 	echo X | dd of=large3 bs=1k seek=2000 &&
 	echo Y | dd of=huge bs=1k seek=2500 &&
-	GIT_ALLOC_LIMIT=1500 &&
+	GIT_ALLOC_LIMIT=1500k &&
 	export GIT_ALLOC_LIMIT
 '
 
diff --git a/wrapper.c b/wrapper.c
index bc1bfb8..69d1c9b 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -11,14 +11,18 @@ static void (*try_to_free_routine)(size_t size) = do_nothing;
 
 static void memory_limit_check(size_t size)
 {
-	static int limit = -1;
-	if (limit == -1) {
-		const char *env = getenv("GIT_ALLOC_LIMIT");
-		limit = env ? atoi(env) * 1024 : 0;
+	static size_t limit = SIZE_MAX;
+	if (limit == SIZE_MAX) {
+		const char *var = "GIT_ALLOC_LIMIT";
+		unsigned long val = 0;
+		const char *env = getenv(var);
+		if (env && !git_parse_ulong(env, &val))
+			die("Failed to parse %s", var);
+		limit = val;
 	}
 	if (limit && size > limit)
-		die("attempting to allocate %"PRIuMAX" over limit %d",
-		    (intmax_t)size, limit);
+		die("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX,
+		    (uintmax_t)size, (uintmax_t)limit);
 }
 
 try_to_free_t set_try_to_free_routine(try_to_free_t routine)
-- 
2.1.0.8.gd3b6067
Previous: Junio C HamanoNext: Jeff King
Message 4 of 15 in “Stream fd to clean filter; GIT_MMAP_LIMIT, GIT_ALLOC_LIMIT with git_parse_ulong()”
  1. 0/4 Stream fd to clean filter; GIT_MMAP_LIMIT, GIT_ALLOC_LIMIT with git_parse_ulong()Steffen Prohaska, Aug 24, 2014
  2. 1/4 convert: Refactor would_convert_to_git() to single arg 'path'Steffen Prohaska, Aug 24, 2014
  3. Junio C HamanoAug 25, 2014
  4. 2/4 Change GIT_ALLOC_LIMIT check to use git_parse_ulong()Steffen Prohaska, Aug 24, 2014
  5. Jeff KingAug 25, 2014
  6. Steffen ProhaskaAug 25, 2014
  7. Jeff KingAug 25, 2014
  8. 3/4 Introduce GIT_MMAP_LIMIT to allow testing expected mmap sizeSteffen Prohaska, Aug 24, 2014
  9. 4/4 convert: Stream from fd to required clean filter instead of mmapSteffen Prohaska, Aug 24, 2014
  10. Jeff KingAug 25, 2014
  11. Steffen ProhaskaAug 25, 2014
  12. Junio C HamanoAug 25, 2014
  13. Jeff KingAug 26, 2014
  14. Junio C HamanoAug 26, 2014
  15. Jeff KingAug 26, 2014

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.