From 2f1c76bf82619e0809a41a1df4fe0e26c6315270 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 6 Jan 2020 13:16:00 +0100 Subject: device: Check for nullptr in Device::enumerate While the fields of the hid_device_info struct should not be null, it is better to perform an explicit check before trying to construct strings from them. --- device.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/device.cc b/device.cc index bc42965..5f26d65 100644 --- a/device.cc +++ b/device.cc @@ -209,14 +209,16 @@ std::vector Device::enumerate(){ auto pInfo_ = pInfo; std::vector res; while (pInfo != nullptr){ - auto deviceModel = product_id_to_model(pInfo->product_id); - if (deviceModel.has_value()) { - std::string path(pInfo->path); - std::wstring serialNumberW(pInfo->serial_number); - std::wstring_convert> converter; - std::string serialNumber = converter.to_bytes(serialNumberW); - DeviceInfo info = { deviceModel.value(), path, serialNumber }; - res.push_back(info); + if (pInfo->path != nullptr && pInfo->serial_number != nullptr) { + auto deviceModel = product_id_to_model(pInfo->product_id); + if (deviceModel.has_value()) { + std::string path(pInfo->path); + std::wstring serialNumberW(pInfo->serial_number); + std::wstring_convert> converter; + std::string serialNumber = converter.to_bytes(serialNumberW); + DeviceInfo info = { deviceModel.value(), path, serialNumber }; + res.push_back(info); + } } pInfo = pInfo->next; } -- cgit v1.2.1 From a3fd24ff87eeae3d320e3ee48f1c6e1b9ba0a783 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 13 Jun 2020 18:55:34 +0200 Subject: Invert if condition for readability --- device.cc | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/device.cc b/device.cc index 5f26d65..1352bc7 100644 --- a/device.cc +++ b/device.cc @@ -209,16 +209,17 @@ std::vector Device::enumerate(){ auto pInfo_ = pInfo; std::vector res; while (pInfo != nullptr){ - if (pInfo->path != nullptr && pInfo->serial_number != nullptr) { - auto deviceModel = product_id_to_model(pInfo->product_id); - if (deviceModel.has_value()) { - std::string path(pInfo->path); - std::wstring serialNumberW(pInfo->serial_number); - std::wstring_convert> converter; - std::string serialNumber = converter.to_bytes(serialNumberW); - DeviceInfo info = { deviceModel.value(), path, serialNumber }; - res.push_back(info); - } + if (pInfo->path == nullptr || pInfo->serial_number == nullptr) { + continue; + } + auto deviceModel = product_id_to_model(pInfo->product_id); + if (deviceModel.has_value()) { + std::string path(pInfo->path); + std::wstring serialNumberW(pInfo->serial_number); + std::wstring_convert> converter; + std::string serialNumber = converter.to_bytes(serialNumberW); + DeviceInfo info = {deviceModel.value(), path, serialNumber}; + res.push_back(info); } pInfo = pInfo->next; } -- cgit v1.2.1