diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-06 13:16:00 +0100 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2020-01-06 13:16:00 +0100 |
commit | 2f1c76bf82619e0809a41a1df4fe0e26c6315270 (patch) | |
tree | 4d77864bf1131798e5523d067d9d6287e3b8f5ad | |
parent | 2c52393d12dbb16ce1d643cf020aff964da6ec89 (diff) | |
download | libnitrokey-enumerate.tar.gz libnitrokey-enumerate.tar.bz2 |
device: Check for nullptr in Device::enumerateenumerate
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.
-rw-r--r-- | device.cc | 18 |
1 files changed, 10 insertions, 8 deletions
@@ -209,14 +209,16 @@ std::vector<DeviceInfo> Device::enumerate(){ auto pInfo_ = pInfo; std::vector<DeviceInfo> 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<std::codecvt_utf8<wchar_t>> 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<std::codecvt_utf8<wchar_t>> converter; + std::string serialNumber = converter.to_bytes(serialNumberW); + DeviceInfo info = { deviceModel.value(), path, serialNumber }; + res.push_back(info); + } } pInfo = pInfo->next; } |