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/errors.c | |
| 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/errors.c')
| -rw-r--r-- | src/errors.c | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/errors.c b/src/errors.c index 5b68dd9cb..3d1d1c9a1 100644 --- a/src/errors.c +++ b/src/errors.c @@ -10,6 +10,7 @@ #include "threadstate.h" #include "posix.h" #include "buffer.h" +#include "libgit2.h" /******************************************** * New error handling @@ -20,6 +21,11 @@ static git_error g_git_oom_error = { GIT_ERROR_NOMEMORY }; +static git_error g_git_uninitialized_error = { + "libgit2 has not been initialized; you must call git_libgit2_init", + GIT_ERROR_INVALID +}; + static void set_error_from_buffer(int error_class) { git_error *error = &GIT_THREADSTATE->error_t; @@ -131,6 +137,10 @@ void git_error_clear(void) const git_error *git_error_last(void) { + /* If the library is not initialized, return a static error. */ + if (!git_libgit2_init_count()) + return &g_git_uninitialized_error; + return GIT_THREADSTATE->last_error; } |
