Skip to content

Commit

Permalink
Check for invalid pcsc card handle before calling SCardStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
xhanulik committed Jul 31, 2023
1 parent 89cc631 commit 8ec1835
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/libopensc/reader-pcsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,20 @@ static int refresh_attributes(sc_reader_t *reader)
reader->flags |= SC_READER_CARD_PRESENT;
}

rv = priv->gpriv->SCardStatus(priv->pcsc_card, NULL, &readers_len, &cstate, &prot, atr, &atr_len);
if (priv->pcsc_card != 0
&& (rv == (LONG)SCARD_W_REMOVED_CARD || rv == (LONG)SCARD_E_INVALID_VALUE || rv == (LONG)SCARD_E_INVALID_HANDLE)) {
/* Timeout should denote no change from previous recorded state,
* but check for valid card handle */
reader->flags &= ~SC_READER_CARD_CHANGED;
if (priv->pcsc_card != 0) {
/* When reader is removed between two subsequent calls to refresh_attributes,
* SCardGetStatusChange does not notice the change - set it here */
reader->flags |= SC_READER_CARD_CHANGED;
* SCardGetStatusChange does not notice the change, test the card handle with SCardStatus */
rv = priv->gpriv->SCardStatus(priv->pcsc_card, NULL, &readers_len, &cstate, &prot, atr, &atr_len);

if (rv == (LONG)SCARD_W_REMOVED_CARD || rv == (LONG)SCARD_E_INVALID_VALUE || rv == (LONG)SCARD_E_INVALID_HANDLE)
reader->flags |= SC_READER_CARD_CHANGED;
/* If this happens, card must be reconnected, otherwise SCardGetStatusChange() will still return timeout
* and card handle will be invalid. */
} else {
/* Otherwise timeout denotes no change from previous recorded state. Make sure that
* changed flag is not set. */
reader->flags &= ~SC_READER_CARD_CHANGED;
}

LOG_FUNC_RETURN(reader->ctx, SC_SUCCESS);
}

Expand Down

0 comments on commit 8ec1835

Please sign in to comment.