diff --git a/scd/apdu.c b/scd/apdu.c --- a/scd/apdu.c +++ b/scd/apdu.c @@ -826,22 +826,21 @@ } +static void +release_pcsc_context (void) +{ + /*log_debug ("%s: releasing context\n", __func__);*/ + pcsc_release_context (pcsc.context); + pcsc.context = 0; +} + static int close_pcsc_reader (int slot) { /*log_debug ("%s: count=%d (ctx=%x)\n", __func__, pcsc.count, pcsc.context);*/ (void)slot; - if (!pcsc.count || !--pcsc.count) - { - int i; - - /*log_debug ("%s: releasing context\n", __func__);*/ - if (pcsc.context) - pcsc_release_context (pcsc.context); - pcsc.context = 0; - for (i = 0; i < MAX_READER; i++) - pcsc.rdrname[i] = NULL; - } + if (!--pcsc.count) + release_pcsc_context (); return 0; } @@ -2041,8 +2040,9 @@ err = gpg_error_from_syserror (); log_error ("error allocating memory for reader list\n"); + if (pcsc.count == 0) + release_pcsc_context (); npth_mutex_unlock (&reader_table_lock); - close_pcsc_reader (0); xfree (dl); return err; } @@ -2053,8 +2053,9 @@ log_error ("pcsc_list_readers failed: %s (0x%lx)\n", pcsc_error_string (r), r); xfree (p); + if (pcsc.count == 0) + release_pcsc_context (); npth_mutex_unlock (&reader_table_lock); - close_pcsc_reader (0); xfree (dl); return iso7816_map_sw (pcsc_error_to_sw (r)); } @@ -2091,12 +2092,8 @@ break; } } - - pcsc.count++; } - npth_mutex_unlock (&reader_table_lock); - *l_p = dl; return 0; } @@ -2113,10 +2110,17 @@ else #endif { /* PC/SC readers. */ + int i; + xfree (dl->table); - close_pcsc_reader (0); + for (i = 0; i < MAX_READER; i++) + pcsc.rdrname[i] = NULL; + + if (pcsc.count == 0) + release_pcsc_context (); } xfree (dl); + npth_mutex_unlock (&reader_table_lock); }