Skip to content

Commit 9927d96

Browse files
sprohaskagitster
authored andcommitted
memory_limit: use git_env_ulong() to parse GIT_ALLOC_LIMIT
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23b0c47 commit 9927d96

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

t/t1050-large.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test_expect_success setup '
1313
echo X | dd of=large2 bs=1k seek=2000 &&
1414
echo X | dd of=large3 bs=1k seek=2000 &&
1515
echo Y | dd of=huge bs=1k seek=2500 &&
16-
GIT_ALLOC_LIMIT=1500 &&
16+
GIT_ALLOC_LIMIT=1500k &&
1717
export GIT_ALLOC_LIMIT
1818
'
1919

wrapper.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ static void (*try_to_free_routine)(size_t size) = do_nothing;
1111

1212
static void memory_limit_check(size_t size)
1313
{
14-
static int limit = -1;
15-
if (limit == -1) {
16-
const char *env = getenv("GIT_ALLOC_LIMIT");
17-
limit = env ? atoi(env) * 1024 : 0;
14+
static size_t limit = 0;
15+
if (!limit) {
16+
limit = git_env_ulong("GIT_ALLOC_LIMIT", 0);
17+
if (!limit)
18+
limit = SIZE_MAX;
1819
}
19-
if (limit && size > limit)
20-
die("attempting to allocate %"PRIuMAX" over limit %d",
21-
(intmax_t)size, limit);
20+
if (size > limit)
21+
die("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX,
22+
(uintmax_t)size, (uintmax_t)limit);
2223
}
2324

2425
try_to_free_t set_try_to_free_routine(try_to_free_t routine)

0 commit comments

Comments
 (0)