summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2017-03-07 11:37:41 +0100
committerSzczepan Zalega <szczepan@nitrokey.com>2017-03-11 15:41:52 +0100
commit257062b9a5130cf25dfd26d4ec93880abde1c9ce (patch)
treea84ccaa669d60e860a97bee9250c4f5c8cf25ec3
parent1b1a3211faa806d656b0ebb50864348c595857ed (diff)
downloadlibnitrokey-257062b9a5130cf25dfd26d4ec93880abde1c9ce.tar.gz
libnitrokey-257062b9a5130cf25dfd26d4ec93880abde1c9ce.tar.bz2
Check for null device reference in case of failed reconnection
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--device.cc20
1 files changed, 9 insertions, 11 deletions
diff --git a/device.cc b/device.cc
index 35e7a78..9a64813 100644
--- a/device.cc
+++ b/device.cc
@@ -77,14 +77,13 @@ int Device::send(const void *packet) {
std::lock_guard<std::mutex> lock(mex_dev_com);
LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2);
- if (mp_devhandle == nullptr) {
- LOG(std::string("Connection fail") , Loglevel::DEBUG_L2);
- throw DeviceNotConnected("Attempted HID send on an invalid descriptor.");
- }
-
int send_feature_report = -1;
for (int i = 0; i < 3 && send_feature_report < 0; ++i) {
+ if (mp_devhandle == nullptr) {
+ LOG(std::string("Connection fail") , Loglevel::DEBUG_L2);
+ throw DeviceNotConnected("Attempted HID send on an invalid descriptor.");
+ }
send_feature_report = hid_send_feature_report(
mp_devhandle, (const unsigned char *)(packet), HID_REPORT_SIZE);
if (send_feature_report < 0) _reconnect();
@@ -101,13 +100,12 @@ int Device::recv(void *packet) {
int status;
int retry_count = 0;
-
- if (mp_devhandle == nullptr){
- LOG(std::string("Connection fail") , Loglevel::DEBUG_L2);
- throw DeviceNotConnected("Attempted HID receive on an invalid descriptor.");
- }
-
for (;;) {
+ if (mp_devhandle == nullptr){
+ LOG(std::string("Connection fail") , Loglevel::DEBUG_L2);
+ throw DeviceNotConnected("Attempted HID receive on an invalid descriptor.");
+ }
+
status = (hid_get_feature_report(mp_devhandle, (unsigned char *)(packet),
HID_REPORT_SIZE));