From 1064d82ea340a05df0f94cfeaa26e7e0533acd93 Mon Sep 17 00:00:00 2001 From: Sascha Kiefer Date: Fri, 13 Sep 2024 10:01:01 +0200 Subject: [PATCH 1/2] fix: avoid endless loop when SCardEstablishContext is not successful --- package.json | 2 +- src/pcsclite.cpp | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index dad5d953..c72eef0b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@pokusew/pcsclite", - "version": "0.6.0", + "version": "0.6.1", "description": "Bindings over PC/SC to access Smart Cards", "keywords": [ "nfc", diff --git a/src/pcsclite.cpp b/src/pcsclite.cpp index dd66881c..4460af35 100644 --- a/src/pcsclite.cpp +++ b/src/pcsclite.cpp @@ -69,13 +69,18 @@ PCSCLite::PCSCLite(): m_card_context(0), LONG result; // TODO: consider removing this do-while Windows workaround that should not be needed anymore + INT retry = 0; do { + if (retry > 0) { + Sleep(100 * retry); + } + // TODO: make dwScope (now hard-coded to SCARD_SCOPE_SYSTEM) customisable result = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &m_card_context); - } while(result == SCARD_E_NO_SERVICE || result == SCARD_E_SERVICE_STOPPED); + } while((result == SCARD_E_NO_SERVICE || result == SCARD_E_SERVICE_STOPPED) && retry++ < 3); if (result != SCARD_S_SUCCESS) { Nan::ThrowError(error_msg("SCardEstablishContext", result).c_str()); } else { From da597145bb6805f9436c26c652d7a209f87cf257 Mon Sep 17 00:00:00 2001 From: Sascha Kiefer Date: Sun, 15 Sep 2024 15:26:41 +0200 Subject: [PATCH 2/2] fix: avoid sleep and while loop and fail fast instead --- src/pcsclite.cpp | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/pcsclite.cpp b/src/pcsclite.cpp index 4460af35..abd541b3 100644 --- a/src/pcsclite.cpp +++ b/src/pcsclite.cpp @@ -67,20 +67,12 @@ PCSCLite::PCSCLite(): m_card_context(0), postServiceCheck: #endif // _WIN32 - LONG result; - // TODO: consider removing this do-while Windows workaround that should not be needed anymore - INT retry = 0; - do { - if (retry > 0) { - Sleep(100 * retry); - } - - // TODO: make dwScope (now hard-coded to SCARD_SCOPE_SYSTEM) customisable - result = SCardEstablishContext(SCARD_SCOPE_SYSTEM, - NULL, - NULL, - &m_card_context); - } while((result == SCARD_E_NO_SERVICE || result == SCARD_E_SERVICE_STOPPED) && retry++ < 3); + // TODO: make dwScope (now hard-coded to SCARD_SCOPE_SYSTEM) customisable + LONG result = SCardEstablishContext(SCARD_SCOPE_SYSTEM, + NULL, + NULL, + &m_card_context); + if (result != SCARD_S_SUCCESS) { Nan::ThrowError(error_msg("SCardEstablishContext", result).c_str()); } else {