diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2020-07-10 08:57:05 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-10-25 16:49:41 +0000 |
commit | 5e3f9e57b6ac3de83bc4e517d411e896abd4e37d (patch) | |
tree | aabb104b5a3723292368bd933a938184792985dc /src/errors.c | |
parent | e3f8928982c044274453111a64d4231c0eb464c4 (diff) | |
download | libgit2-ethomson/friendlier_getting_started.tar.gz |
alloc: set up an allocator that fails before library initethomson/friendlier_getting_started
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 d4da50da8..95a720b9a 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; } |