From 3a8b69d13da66a0555231c84e71df010129b5afb Mon Sep 17 00:00:00 2001 From: Leo Yang Date: Thu, 26 Feb 2015 11:47:07 -0500 Subject: [PATCH 1/3] Fix leak of TLS error message in shutdown (ptherad version) --- src/global.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/global.c b/src/global.c index fcbcbb176..ee4c929dc 100644 --- a/src/global.c +++ b/src/global.c @@ -263,9 +263,7 @@ int init_error = 0; static void cb__free_status(void *st) { - git_global_st *state = (git_global_st *) st; - git__free(state->error_t.message); - + giterr_clear(); git__free(st); } @@ -308,6 +306,8 @@ int git_libgit2_shutdown(void) /* Shut down any subsystems that have global state */ git__shutdown(); + giterr_clear(); + ptr = pthread_getspecific(_tls_key); pthread_setspecific(_tls_key, NULL); git__free(ptr); From 83fe60fa1bb7c8293585e6e8d6b12f3039910bbb Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 3 Mar 2015 14:10:50 -0500 Subject: [PATCH 2/3] libgit2_shutdown: clear err message on shutdown Clear the error message on git_libgit2_shutdown for all versions of the library (no threads and Win32 threads). Drop the giterr_clear in clar, as that shouldn't be necessary. --- src/global.c | 36 +++++++++++++++++++++++++++--------- tests/main.c | 1 - 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/global.c b/src/global.c index ee4c929dc..2f31c7de4 100644 --- a/src/global.c +++ b/src/global.c @@ -36,16 +36,25 @@ void git__on_shutdown(git_global_shutdown_fn callback) git__shutdown_callbacks[count - 1] = callback; } +static void git__global_state_cleanup(git_global_st *st) +{ + if (!st) + return; + + git__free(st->error_t.message); + st->error_t.message = NULL; +} + static void git__shutdown(void) { int pos; + /* Shutdown subsystems that have registered */ for (pos = git_atomic_get(&git__n_shutdown_callbacks); pos > 0; pos = git_atomic_dec(&git__n_shutdown_callbacks)) { git_global_shutdown_fn cb = git__swap(git__shutdown_callbacks[pos - 1], NULL); if (cb != NULL) cb(); } - } #if defined(GIT_THREADS) && defined(GIT_SSL) @@ -214,8 +223,14 @@ int git_libgit2_init(void) static void synchronized_threads_shutdown(void) { + void *ptr; + /* Shut down any subsystems that have global state */ git__shutdown(); + + ptr = TlsGetValue(_tls_index); + git__global_state_cleanup(ptr); + TlsFree(_tls_index); git_mutex_free(&git__mwindow_mutex); } @@ -263,7 +278,7 @@ int init_error = 0; static void cb__free_status(void *st) { - giterr_clear(); + git__global_state_cleanup(st); git__free(st); } @@ -300,23 +315,23 @@ int git_libgit2_shutdown(void) pthread_once_t new_once = PTHREAD_ONCE_INIT; int ret; - if ((ret = git_atomic_dec(&git__n_inits)) > 0) + if ((ret = git_atomic_dec(&git__n_inits)) != 0) return ret; /* Shut down any subsystems that have global state */ git__shutdown(); - giterr_clear(); - ptr = pthread_getspecific(_tls_key); pthread_setspecific(_tls_key, NULL); + + git__global_state_cleanup(ptr); git__free(ptr); pthread_key_delete(_tls_key); git_mutex_free(&git__mwindow_mutex); _once_init = new_once; - return ret; + return 0; } git_global_st *git__global_state(void) @@ -358,10 +373,13 @@ int git_libgit2_shutdown(void) int ret; /* Shut down any subsystems that have global state */ - if (ret = git_atomic_dec(&git__n_inits)) - git__shutdown(); + if ((ret = git_atomic_dec(&git__n_inits)) != 0) + return ret; - return ret; + git__shutdown(); + git__global_state_cleanup(&__state); + + return 0; } git_global_st *git__global_state(void) diff --git a/tests/main.c b/tests/main.c index cc1323083..f67c8ffbc 100644 --- a/tests/main.c +++ b/tests/main.c @@ -20,7 +20,6 @@ int main(int argc, char *argv[]) clar_test_shutdown(); - giterr_clear(); cl_global_trace_disable(); git_libgit2_shutdown(); From 8e851c1e8c8f92da41d44e15e0c75777e4d1110e Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 3 Mar 2015 16:41:59 -0500 Subject: [PATCH 3/3] libgit2_shutdown: free TLS data (win32) Free TLS data on thread exit (win32) --- src/global.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/global.c b/src/global.c index 2f31c7de4..1f3432d09 100644 --- a/src/global.c +++ b/src/global.c @@ -270,6 +270,17 @@ git_global_st *git__global_state(void) return ptr; } +BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved) +{ + if (reason == DLL_THREAD_DETACH) { + void *ptr = TlsGetValue(_tls_index); + git__global_state_cleanup(ptr); + git__free(ptr); + } + + return TRUE; +} + #elif defined(GIT_THREADS) && defined(_POSIX_THREADS) static pthread_key_t _tls_key;