]>
git.proxmox.com Git - libgit2.git/blob - tests/buf/oom.c
1 #include "clar_libgit2.h"
4 /* Override default allocators with ones that will fail predictably. */
6 static git_allocator std_alloc
;
7 static git_allocator oom_alloc
;
9 static void *oom_malloc(size_t n
, const char *file
, int line
)
11 /* Reject any allocation of more than 100 bytes */
12 return (n
> 100) ? NULL
: std_alloc
.gmalloc(n
, file
, line
);
15 static void *oom_realloc(void *p
, size_t n
, const char *file
, int line
)
17 /* Reject any allocation of more than 100 bytes */
18 return (n
> 100) ? NULL
: std_alloc
.grealloc(p
, n
, file
, line
);
21 void test_buf_oom__initialize(void)
23 git_stdalloc_init_allocator(&std_alloc
);
24 git_stdalloc_init_allocator(&oom_alloc
);
26 oom_alloc
.gmalloc
= oom_malloc
;
27 oom_alloc
.grealloc
= oom_realloc
;
29 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR
, &oom_alloc
));
32 void test_buf_oom__cleanup(void)
34 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR
, NULL
));
37 void test_buf_oom__grow(void)
39 git_buf buf
= GIT_BUF_INIT
;
41 cl_git_pass(git_buf_grow(&buf
, 42));
42 cl_assert(!git_buf_oom(&buf
));
44 cl_assert(git_buf_grow(&buf
, 101) == -1);
45 cl_assert(git_buf_oom(&buf
));
47 git_buf_dispose(&buf
);
50 void test_buf_oom__grow_by(void)
52 git_buf buf
= GIT_BUF_INIT
;
54 cl_git_pass(git_buf_grow_by(&buf
, 42));
55 cl_assert(!git_buf_oom(&buf
));
57 cl_assert(git_buf_grow_by(&buf
, 101) == -1);
58 cl_assert(git_buf_oom(&buf
));