From 08f28ff50e3f6fabcc1469fdf4a53c3a3a33eff4 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 10 Jul 2020 08:57:05 +0100 Subject: alloc: set up an allocator that fails before library init We require the library to be initialized with git_libgit2_init before it is functional. However, if a user tries to uses the library without doing so - as they might when getting started with the library for the first time - we will likely crash. This commit introduces some guard rails - now instead of having _no_ allocator by default, we'll have an allocator that always fails, and never tries to set an error message (since the thread-local state is set up by git_libgit2_init). We've modified the error retrieval function to (try to) ensure that the library has been initialized before getting the thread-local error message. (Unfortunately, we cannot determine if the thread local storage has actually been configured, this does require initialization by git_libgit2_init. But a naive attempt should be good enough for most cases.) --- src/allocators/failalloc.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/allocators/failalloc.c (limited to 'src/allocators/failalloc.c') diff --git a/src/allocators/failalloc.c b/src/allocators/failalloc.c new file mode 100644 index 000000000..5257d1dec --- /dev/null +++ b/src/allocators/failalloc.c @@ -0,0 +1,92 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "failalloc.h" + +void *git_failalloc_malloc(size_t len, const char *file, int line) +{ + GIT_UNUSED(len); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +void *git_failalloc_calloc(size_t nelem, size_t elsize, const char *file, int line) +{ + GIT_UNUSED(nelem); + GIT_UNUSED(elsize); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +char *git_failalloc_strdup(const char *str, const char *file, int line) +{ + GIT_UNUSED(str); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +char *git_failalloc_strndup(const char *str, size_t n, const char *file, int line) +{ + GIT_UNUSED(str); + GIT_UNUSED(n); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +char *git_failalloc_substrdup(const char *start, size_t n, const char *file, int line) +{ + GIT_UNUSED(start); + GIT_UNUSED(n); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +void *git_failalloc_realloc(void *ptr, size_t size, const char *file, int line) +{ + GIT_UNUSED(ptr); + GIT_UNUSED(size); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +void *git_failalloc_reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line) +{ + GIT_UNUSED(ptr); + GIT_UNUSED(nelem); + GIT_UNUSED(elsize); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +void *git_failalloc_mallocarray(size_t nelem, size_t elsize, const char *file, int line) +{ + GIT_UNUSED(nelem); + GIT_UNUSED(elsize); + GIT_UNUSED(file); + GIT_UNUSED(line); + + return NULL; +} + +void git_failalloc_free(void *ptr) +{ + GIT_UNUSED(ptr); +} -- cgit v1.2.1