]> git.proxmox.com Git - libgit2.git/blob - tests/buf/oom.c
16a03cc1a3a64734204e6fcab06c070ba7dbb5ba
[libgit2.git] / tests / buf / oom.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
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(__linux__)
15 # define TOOBIG 0x0fffffff
16 #elif defined(GIT_ARCH_64)
17 # define TOOBIG 0xffffffffffffff00
18 #else
19 # define TOOBIG 0xffffff00
20 #endif
21
22 /**
23 * If we make a ridiculously large request the first time we
24 * actually allocate some space in the git_buf, the realloc()
25 * will fail. And because the git_buf_grow() wrapper always
26 * sets mark_oom, the code in git_buf_try_grow() will free
27 * the internal buffer and set it to git_buf__oom.
28 *
29 * We initialized the internal buffer to (the static variable)
30 * git_buf__initbuf. The purpose of this test is to make sure
31 * that we don't try to free the static buffer.
32 */
33 void test_buf_oom__grow(void)
34 {
35 git_buf buf = GIT_BUF_INIT;
36
37 git_buf_clear(&buf);
38
39 cl_assert(git_buf_grow(&buf, TOOBIG) == -1);
40 cl_assert(git_buf_oom(&buf));
41
42 git_buf_free(&buf);
43 }
44
45 void test_buf_oom__grow_by(void)
46 {
47 git_buf buf = GIT_BUF_INIT;
48
49 buf.size = SIZE_MAX-10;
50
51 cl_assert(git_buf_grow_by(&buf, 50) == -1);
52 cl_assert(git_buf_oom(&buf));
53 }