Blame tests/buf/oom.c

Packit ae9e2a
#include "clar_libgit2.h"
Packit ae9e2a
#include "buffer.h"
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
 * We want to use some ridiculous size that `malloc` will fail with
Packit ae9e2a
 * but that does not otherwise interfere with testing.  On Linux, choose
Packit ae9e2a
 * a number that is large enough to fail immediately but small enough
Packit ae9e2a
 * that valgrind doesn't believe it to erroneously be a negative number.
Packit ae9e2a
 * On macOS, choose a number that is large enough to fail immediately
Packit ae9e2a
 * without having libc print warnings to stderr.
Packit ae9e2a
 */
Packit ae9e2a
#if defined(GIT_ARCH_64) && defined(__linux__)
Packit ae9e2a
# define TOOBIG 0x0fffffffffffffff
Packit ae9e2a
#elif defined(__linux__)
Packit 623ad7
# define TOOBIG 0xffffff00
Packit ae9e2a
#elif defined(GIT_ARCH_64)
Packit ae9e2a
# define TOOBIG 0xffffffffffffff00
Packit ae9e2a
#else
Packit ae9e2a
# define TOOBIG 0xffffff00
Packit ae9e2a
#endif
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * If we make a ridiculously large request the first time we
Packit ae9e2a
 * actually allocate some space in the git_buf, the realloc()
Packit ae9e2a
 * will fail.  And because the git_buf_grow() wrapper always
Packit ae9e2a
 * sets mark_oom, the code in git_buf_try_grow() will free
Packit ae9e2a
 * the internal buffer and set it to git_buf__oom.
Packit ae9e2a
 * 
Packit ae9e2a
 * We initialized the internal buffer to (the static variable)
Packit ae9e2a
 * git_buf__initbuf.  The purpose of this test is to make sure
Packit ae9e2a
 * that we don't try to free the static buffer.
Packit ae9e2a
 */
Packit ae9e2a
void test_buf_oom__grow(void)
Packit ae9e2a
{
Packit ae9e2a
	git_buf buf = GIT_BUF_INIT;
Packit ae9e2a
Packit ae9e2a
	git_buf_clear(&buf;;
Packit ae9e2a
Packit ae9e2a
	cl_assert(git_buf_grow(&buf, TOOBIG) == -1);
Packit ae9e2a
	cl_assert(git_buf_oom(&buf));
Packit ae9e2a
Packit ae9e2a
	git_buf_free(&buf;;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void test_buf_oom__grow_by(void)
Packit ae9e2a
{
Packit ae9e2a
	git_buf buf = GIT_BUF_INIT;
Packit ae9e2a
Packit ae9e2a
	buf.size = SIZE_MAX-10;
Packit ae9e2a
Packit ae9e2a
	cl_assert(git_buf_grow_by(&buf, 50) == -1);
Packit ae9e2a
	cl_assert(git_buf_oom(&buf));
Packit ae9e2a
}