Skip to content

Commit 1194546

Browse files
authored
Merge pull request libgit2#4854 from libgit2/ethomson/buf_oom_test
buf::oom tests: use custom allocator for oom failures
2 parents 671b244 + 2e34efa commit 1194546

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

include/git2/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ typedef enum {
364364
*
365365
* > Set the memory allocator to a different memory allocator. This
366366
* > allocator will then be used to make all memory allocations for
367-
* > libgit2 operations.
367+
* > libgit2 operations. If the given `allocator` is NULL, then the
368+
* > system default will be restored.
368369
*
369370
* opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, int enabled)
370371
*

src/alloc.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515

1616
git_allocator git__allocator;
1717

18+
static int setup_default_allocator(void)
19+
{
20+
#if defined(GIT_MSVC_CRTDBG)
21+
return git_win32_crtdbg_init_allocator(&git__allocator);
22+
#else
23+
return git_stdalloc_init_allocator(&git__allocator);
24+
#endif
25+
}
26+
1827
int git_allocator_global_init(void)
1928
{
2029
/*
@@ -24,15 +33,14 @@ int git_allocator_global_init(void)
2433
if (git__allocator.gmalloc != NULL)
2534
return 0;
2635

27-
#if defined(GIT_MSVC_CRTDBG)
28-
return git_win32_crtdbg_init_allocator(&git__allocator);
29-
#else
30-
return git_stdalloc_init_allocator(&git__allocator);
31-
#endif
36+
return setup_default_allocator();
3237
}
3338

3439
int git_allocator_setup(git_allocator *allocator)
3540
{
41+
if (!allocator)
42+
return setup_default_allocator();
43+
3644
memcpy(&git__allocator, allocator, sizeof(*allocator));
3745
return 0;
3846
}

tests/buf/oom.c

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
11
#include "clar_libgit2.h"
22
#include "buffer.h"
33

4-
/*
5-
* We want to use some ridiculous size that `malloc` will fail with
6-
* but that does not otherwise interfere with testing. On Linux, choose
7-
* a number that is large enough to fail immediately but small enough
8-
* that valgrind doesn't believe it to erroneously be a negative number.
9-
* On macOS, choose a number that is large enough to fail immediately
10-
* without having libc print warnings to stderr.
11-
*/
12-
#if defined(GIT_ARCH_64) && defined(__linux__)
13-
# define TOOBIG 0x0fffffffffffffff
14-
#elif defined(GIT_ARCH_64)
15-
# define TOOBIG 0xffffffffffffff00
16-
#endif
17-
18-
/**
19-
* If we make a ridiculously large request the first time we
20-
* actually allocate some space in the git_buf, the realloc()
21-
* will fail. And because the git_buf_grow() wrapper always
22-
* sets mark_oom, the code in git_buf_try_grow() will free
23-
* the internal buffer and set it to git_buf__oom.
24-
*
25-
* We initialized the internal buffer to (the static variable)
26-
* git_buf__initbuf. The purpose of this test is to make sure
27-
* that we don't try to free the static buffer.
28-
*
29-
* Skip this test entirely on 32-bit platforms; a buffer large enough
30-
* to guarantee malloc failures is so large that valgrind considers
31-
* it likely to be an error.
32-
*/
4+
/* Override default allocators with ones that will fail predictably. */
5+
6+
static git_allocator std_alloc;
7+
static git_allocator oom_alloc;
8+
9+
static void *oom_malloc(size_t n, const char *file, int line)
10+
{
11+
/* Reject any allocation of more than 100 bytes */
12+
return (n > 100) ? NULL : std_alloc.gmalloc(n, file, line);
13+
}
14+
15+
static void *oom_realloc(void *p, size_t n, const char *file, int line)
16+
{
17+
/* Reject any allocation of more than 100 bytes */
18+
return (n > 100) ? NULL : std_alloc.grealloc(p, n, file, line);
19+
}
20+
21+
void test_buf_oom__initialize(void)
22+
{
23+
git_stdalloc_init_allocator(&std_alloc);
24+
git_stdalloc_init_allocator(&oom_alloc);
25+
26+
oom_alloc.gmalloc = oom_malloc;
27+
oom_alloc.grealloc = oom_realloc;
28+
29+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, &oom_alloc));
30+
}
31+
32+
void test_buf_oom__cleanup(void)
33+
{
34+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, NULL));
35+
}
36+
3337
void test_buf_oom__grow(void)
3438
{
35-
#ifdef GIT_ARCH_64
3639
git_buf buf = GIT_BUF_INIT;
3740

38-
git_buf_clear(&buf);
41+
cl_git_pass(git_buf_grow(&buf, 42));
42+
cl_assert(!git_buf_oom(&buf));
3943

40-
cl_assert(git_buf_grow(&buf, TOOBIG) == -1);
44+
cl_assert(git_buf_grow(&buf, 101) == -1);
4145
cl_assert(git_buf_oom(&buf));
4246

4347
git_buf_dispose(&buf);
44-
#else
45-
cl_skip();
46-
#endif
4748
}
4849

4950
void test_buf_oom__grow_by(void)
5051
{
5152
git_buf buf = GIT_BUF_INIT;
5253

53-
buf.size = SIZE_MAX-10;
54+
cl_git_pass(git_buf_grow_by(&buf, 42));
55+
cl_assert(!git_buf_oom(&buf));
5456

55-
cl_assert(git_buf_grow_by(&buf, 50) == -1);
57+
cl_assert(git_buf_grow_by(&buf, 101) == -1);
5658
cl_assert(git_buf_oom(&buf));
5759
}

0 commit comments

Comments
 (0)