]>
git.proxmox.com Git - libgit2.git/blob - tests/buf/oom.c
16a03cc1a3a64734204e6fcab06c070ba7dbb5ba
1 #include "clar_libgit2.h"
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.
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
19 # define TOOBIG 0xffffff00
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.
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.
33 void test_buf_oom__grow(void)
35 git_buf buf
= GIT_BUF_INIT
;
39 cl_assert(git_buf_grow(&buf
, TOOBIG
) == -1);
40 cl_assert(git_buf_oom(&buf
));
45 void test_buf_oom__grow_by(void)
47 git_buf buf
= GIT_BUF_INIT
;
49 buf
.size
= SIZE_MAX
-10;
51 cl_assert(git_buf_grow_by(&buf
, 50) == -1);
52 cl_assert(git_buf_oom(&buf
));