Skip to content

Conversation

@ethomson
Copy link
Member

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 PR attempts to set up some guard rails to help a user out in this case.

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.)

Of course, this pre-supposes that the first program a user builds will actually check the error message. I'm not certain that this is generally true. I actually often write a quick little program and forget to call git_libgit2_init (but do error checking). But I don't think that I'm a very typical user of the library.

It might be valuable to fprintf(stderr, "hey! call git_libgit2_init!\n")...? But I thought that I'd start with this PR here and iterate.

Depends on #5546

@ethomson ethomson force-pushed the ethomson/friendlier_getting_started branch 2 times, most recently from e5af50a to 158acea Compare July 12, 2020 12:08
@ethomson ethomson force-pushed the ethomson/friendlier_getting_started branch from 158acea to 5e3f9e5 Compare October 25, 2020 16:49
@ethomson ethomson force-pushed the ethomson/friendlier_getting_started branch 2 times, most recently from 654d2e5 to e95fb02 Compare December 9, 2020 13:26
A function to provide the initialization count of the library; this is
subject to race conditions but is useful for a naive determination as to
whether the library has been initialized or not.
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.)
@ethomson ethomson force-pushed the ethomson/friendlier_getting_started branch from e95fb02 to 08f28ff Compare December 9, 2020 13:37
@ethomson
Copy link
Member Author

ethomson commented Dec 9, 2020

Given:

#include <stdio.h>
#include <git2.h>

int main(int argc, char **argv)
{
	git_repository *foo;

	printf("%d\n", git_repository_open(&foo, "."));
	printf("%s\n", git_error_last()->message);
}

Before: (crash)

After:

-1
libgit2 has not been initialized; you must call git_libgit2_init

@ethomson ethomson merged commit b661e9a into master Dec 9, 2020
@ethomson ethomson deleted the ethomson/friendlier_getting_started branch December 9, 2020 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant