Skip to content

Commit 0271022

Browse files
sprohaskagitster
authored andcommitted
mmap_limit: introduce GIT_MMAP_LIMIT to allow testing expected mmap size
In order to test expectations about mmap in a way similar to testing expectations about malloc with GIT_ALLOC_LIMIT introduced by d41489a (Add more large blob test cases, 2012-03-07), introduce a new environment variable GIT_MMAP_LIMIT to limit the largest allowed mmap length. xmmap() is modified to check the size of the requested region and fail it if it is beyond the limit. Together with GIT_ALLOC_LIMIT tests can now confirm expectations about memory consumption. Signed-off-by: Steffen Prohaska <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9927d96 commit 0271022

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

sha1_file.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,26 @@ void release_pack_memory(size_t need)
663663
; /* nothing */
664664
}
665665

666+
static void mmap_limit_check(size_t length)
667+
{
668+
static size_t limit = 0;
669+
if (!limit) {
670+
limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
671+
if (!limit)
672+
limit = SIZE_MAX;
673+
}
674+
if (length > limit)
675+
die("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX,
676+
(uintmax_t)length, (uintmax_t)limit);
677+
}
678+
666679
void *xmmap(void *start, size_t length,
667680
int prot, int flags, int fd, off_t offset)
668681
{
669-
void *ret = mmap(start, length, prot, flags, fd, offset);
682+
void *ret;
683+
684+
mmap_limit_check(length);
685+
ret = mmap(start, length, prot, flags, fd, offset);
670686
if (ret == MAP_FAILED) {
671687
if (!length)
672688
return NULL;

0 commit comments

Comments
 (0)