]> git.ipfire.org Git - thirdparty/git.git/commitdiff
memory_limit: use git_env_ulong() to parse GIT_ALLOC_LIMIT
authorSteffen Prohaska <prohaska@zib.de>
Tue, 26 Aug 2014 15:23:22 +0000 (17:23 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Aug 2014 17:25:04 +0000 (10:25 -0700)
GIT_ALLOC_LIMIT limits xmalloc()'s size, which is of type size_t.
Better use git_env_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 the environment variable is only meant to
be used for our own tests, and we use it 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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t1050-large.sh
wrapper.c

index aea493646e4400e733749768e48f2a3d1a470299..e7657ab323647c9cc9587c0d336ae10adfe8d0d3 100755 (executable)
@@ -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
 '
 
index bc1bfb86003cb4133cc4ce3ce6423ce780ed7c84..c5204f7a07bd011df913c888475746834df79d7d 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -11,14 +11,15 @@ 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 = 0;
+       if (!limit) {
+               limit = git_env_ulong("GIT_ALLOC_LIMIT", 0);
+               if (!limit)
+                       limit = SIZE_MAX;
        }
-       if (limit && size > limit)
-               die("attempting to allocate %"PRIuMAX" over limit %d",
-                   (intmax_t)size, limit);
+       if (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)