From 2c0d1b9ab0bd49a5252415d6a91324977a4ba0ce Mon Sep 17 00:00:00 2001 From: Frank Morgner Date: Thu, 5 Jul 2018 14:02:16 +0200 Subject: [PATCH] reset sc_card_t during card detection fixes https://github.com/OpenSC/OpenSC/issues/1417 --- src/libopensc/card.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libopensc/card.c b/src/libopensc/card.c index 5968b0bd..99b8da61 100644 --- a/src/libopensc/card.c +++ b/src/libopensc/card.c @@ -261,8 +261,22 @@ int sc_connect_card(sc_reader_t *reader, sc_card_t **card_out) } } else { + sc_card_t uninitialized = *card; sc_log(ctx, "matching built-in ATRs"); for (i = 0; ctx->card_drivers[i] != NULL; i++) { + /* FIXME If we had a clean API description, we'd propably get a + * cleaner implementation of the driver's match_card and init, + * which should normally *not* modify the card object if + * unsuccessful. However, after years of relentless hacking, reality + * is different: The card object is changed in virtually every card + * driver so in order to prevent unwanted interaction, we reset the + * card object here and hope that the card driver at least doesn't + * allocate any internal ressources that need to be freed. If we + * had more time, we should refactor the existing code to not + * modify sc_card_t until complete success (possibly by combining + * `match_card()` and `init()`) */ + *card = uninitialized; + struct sc_card_driver *drv = ctx->card_drivers[i]; const struct sc_card_operations *ops = drv->ops;