diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2020-07-10 08:57:05 +0100 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-12-09 13:36:28 +0000 |
| commit | 08f28ff50e3f6fabcc1469fdf4a53c3a3a33eff4 (patch) | |
| tree | eef5cf32487f23de17be31abfe944ee7b6c0fbfe /src/allocators/failalloc.h | |
| parent | 6c51014db6699b21a39641d4a5c01d76d1f1d8f5 (diff) | |
| download | libgit2-08f28ff50e3f6fabcc1469fdf4a53c3a3a33eff4.tar.gz | |
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.)
Diffstat (limited to 'src/allocators/failalloc.h')
| -rw-r--r-- | src/allocators/failalloc.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/allocators/failalloc.h b/src/allocators/failalloc.h new file mode 100644 index 000000000..6115e51e7 --- /dev/null +++ b/src/allocators/failalloc.h @@ -0,0 +1,23 @@ +/* + * 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. + */ + +#ifndef INCLUDE_allocators_failalloc_h__ +#define INCLUDE_allocators_failalloc_h__ + +#include "common.h" + +extern void *git_failalloc_malloc(size_t len, const char *file, int line); +extern void *git_failalloc_calloc(size_t nelem, size_t elsize, const char *file, int line); +extern char *git_failalloc_strdup(const char *str, const char *file, int line); +extern char *git_failalloc_strndup(const char *str, size_t n, const char *file, int line); +extern char *git_failalloc_substrdup(const char *start, size_t n, const char *file, int line); +extern void *git_failalloc_realloc(void *ptr, size_t size, const char *file, int line); +extern void *git_failalloc_reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line); +extern void *git_failalloc_mallocarray(size_t nelem, size_t elsize, const char *file, int line); +extern void git_failalloc_free(void *ptr); + +#endif |
