|
1 | 1 | #include "clar_libgit2.h" |
2 | 2 | #include "buffer.h" |
3 | 3 |
|
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 | + |
33 | 37 | void test_buf_oom__grow(void) |
34 | 38 | { |
35 | | -#ifdef GIT_ARCH_64 |
36 | 39 | git_buf buf = GIT_BUF_INIT; |
37 | 40 |
|
38 | | - git_buf_clear(&buf); |
| 41 | + cl_git_pass(git_buf_grow(&buf, 42)); |
| 42 | + cl_assert(!git_buf_oom(&buf)); |
39 | 43 |
|
40 | | - cl_assert(git_buf_grow(&buf, TOOBIG) == -1); |
| 44 | + cl_assert(git_buf_grow(&buf, 101) == -1); |
41 | 45 | cl_assert(git_buf_oom(&buf)); |
42 | 46 |
|
43 | 47 | git_buf_dispose(&buf); |
44 | | -#else |
45 | | - cl_skip(); |
46 | | -#endif |
47 | 48 | } |
48 | 49 |
|
49 | 50 | void test_buf_oom__grow_by(void) |
50 | 51 | { |
51 | 52 | git_buf buf = GIT_BUF_INIT; |
52 | 53 |
|
53 | | - buf.size = SIZE_MAX-10; |
| 54 | + cl_git_pass(git_buf_grow_by(&buf, 42)); |
| 55 | + cl_assert(!git_buf_oom(&buf)); |
54 | 56 |
|
55 | | - cl_assert(git_buf_grow_by(&buf, 50) == -1); |
| 57 | + cl_assert(git_buf_grow_by(&buf, 101) == -1); |
56 | 58 | cl_assert(git_buf_oom(&buf)); |
57 | 59 | } |
0 commit comments