diff options
author | Anderson Toshiyuki Sasaki <ansasaki@redhat.com> | 2018-08-13 14:18:07 +0200 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2018-08-13 15:27:51 +0200 |
commit | b99849c831bae1d7993d724cc706afc6bb05086a (patch) | |
tree | 3ba4a546e185affece93b5e4e011418d5a1c0638 | |
parent | c7d4286ca1e97beebe67dfb2207f053c4c9daae0 (diff) | |
download | libssh-b99849c831bae1d7993d724cc706afc6bb05086a.tar.gz libssh-b99849c831bae1d7993d724cc706afc6bb05086a.tar.xz libssh-b99849c831bae1d7993d724cc706afc6bb05086a.zip |
init: ignore init counter if destructor calls finalize
If the destructor calls finalize, ignore the init counter and finalize
the library anyway.
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit 66a3bc0332cda91484dc02a393b7e6701df218b8)
-rw-r--r-- | src/init.c | 40 |
1 files changed, 16 insertions, 24 deletions
@@ -153,29 +153,29 @@ static int _ssh_finalize(unsigned destructor) { if (!destructor) { ssh_mutex_lock(&ssh_init_mutex); - } - - if (_ssh_initialized == 1) { - _ssh_initialized = 0; - if (_ssh_init_ret < 0) { + if (_ssh_initialized > 1) { + _ssh_initialized--; goto _ret; } - ssh_dh_finalize(); - ssh_crypto_finalize(); - ssh_socket_cleanup(); - /* It is important to finalize threading after CRYPTO because - * it still depends on it */ - ssh_threads_finalize(); - - } - else { - if (_ssh_initialized > 0) { - _ssh_initialized--; + if (_ssh_initialized == 1) { + if (_ssh_init_ret < 0) { + goto _ret; + } } } + /* If the counter reaches zero or it is the destructor calling, finalize */ + ssh_dh_finalize(); + ssh_crypto_finalize(); + ssh_socket_cleanup(); + /* It is important to finalize threading after CRYPTO because + * it still depends on it */ + ssh_threads_finalize(); + + _ssh_initialized = 0; + _ret: if (!destructor) { ssh_mutex_unlock(&ssh_init_mutex); @@ -200,14 +200,6 @@ void libssh_destructor(void) if (rc < 0) { fprintf(stderr, "Error in libssh_destructor()\n"); } - - /* Detect if ssh_init() was called without matching ssh_finalize() */ - if (_ssh_initialized > 0) { - fprintf(stderr, - "Warning: ssh still initialized; probably ssh_init() " - "was called more than once (init count: %d)\n", - _ssh_initialized); - } } /** |