From 8bf31924265baf81372fe42580dee4064a642375 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Tue, 23 Jan 2024 09:28:26 +0100 Subject: [PATCH] sss-client: handle key value in destructor When the pthread key destructor is called the key value is already set to NULL by the caller. As a result the data stored in the value can only be accessed by the first argument passed to the destructor and not by pthread_getspecific() as the previous code did. Resolves: https://github.com/SSSD/sssd/issues/7189 Reviewed-by: Alexey Tikhonov Reviewed-by: Iker Pedrosa (cherry picked from commit b439847bc88ad7b89f0596af822c0ffbf2a579df) --- src/sss_client/common.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/sss_client/common.c b/src/sss_client/common.c index 702d0597d..32555edf3 100644 --- a/src/sss_client/common.c +++ b/src/sss_client/common.c @@ -93,8 +93,22 @@ void sss_cli_close_socket(void) #ifdef HAVE_PTHREAD_EXT static void sss_at_thread_exit(void *v) { - sss_cli_close_socket(); + /* At this point the key value is already set to NULL and the only way to + * access the data from the value is via the argument passed to the + * destructor (sss_at_thread_exit). See e.g. + * https://www.man7.org/linux/man-pages/man3/pthread_key_create.3p.html + * for details. */ + + struct sss_socket_descriptor_t *descriptor = (struct sss_socket_descriptor_t *) v; + + if (descriptor->sd != -1) { + close(descriptor->sd); + descriptor->sd = -1; + } + free(v); + + /* Most probably redudant, but better safe than sorry. */ pthread_setspecific(sss_sd_key, NULL); } -- 2.42.0