Some side tracking fixing undefined behaviour and memory vulnurabilities
This commit is contained in:
@@ -20,9 +20,11 @@
|
||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "libusb.h"
|
||||
#include "libusbwrap/UsbDevice.hpp"
|
||||
@@ -30,50 +32,53 @@
|
||||
|
||||
namespace libusbwrap {
|
||||
|
||||
UsbDeviceFactory::~UsbDeviceFactory() {
|
||||
if (mDeviceListInitialized) {
|
||||
libusb_free_device_list(mLibusbDeviceList, 1);
|
||||
}
|
||||
}
|
||||
UsbDeviceFactory::~UsbDeviceFactory() = default;
|
||||
|
||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
||||
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
||||
refreshDeviceList();
|
||||
return buildMaskedDeviceVector(0x0, 0x0, 0x0, 0x0);
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
|
||||
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
|
||||
refreshDeviceList();
|
||||
return buildMaskedDeviceVector(0xffff, 0xffff, vid, pid);
|
||||
return buildMaskedDeviceVector(LIBUSB_BITMASK_ALL, LIBUSB_BITMASK_ALL, vid, pid);
|
||||
}
|
||||
|
||||
int UsbDeviceFactory::refreshDeviceList() {
|
||||
int ret = libusb_get_device_list(mLibusbCtx, &mLibusbDeviceList);
|
||||
ssize_t UsbDeviceFactory::refreshDeviceList() {
|
||||
libusb_device** list{nullptr};
|
||||
|
||||
ssize_t ret = libusb_get_device_list(mLibusbCtx, &list);
|
||||
mLibusbDeviceList.clear();
|
||||
if (ret < 0) {
|
||||
spdlog::error("Error enumarating USB devices");
|
||||
} else if (ret == 0) {
|
||||
spdlog::warn("No USB devices found");
|
||||
}
|
||||
mDeviceListInitialized = true;
|
||||
|
||||
for (ssize_t i = 0; i < ret; i++) {
|
||||
mLibusbDeviceList.emplace_back(list[i]);
|
||||
}
|
||||
|
||||
libusb_free_device_list(list, false);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask,
|
||||
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask,
|
||||
uint16_t pidMask,
|
||||
uint16_t vid,
|
||||
uint16_t pid) {
|
||||
std::vector<std::shared_ptr<IUsbDevice>> matchedDevices;
|
||||
std::vector<std::unique_ptr<IUsbDevice>> matchedDevices;
|
||||
// see libusb/examples/listdevs.c
|
||||
int i = 0;
|
||||
libusb_device* currDev = nullptr;
|
||||
while ((currDev = mLibusbDeviceList[i++]) != nullptr) {
|
||||
struct libusb_device_descriptor currDevDesc;
|
||||
int ret = libusb_get_device_descriptor(currDev, &currDevDesc);
|
||||
for (auto& currDev : mLibusbDeviceList) {
|
||||
struct libusb_device_descriptor currDevDesc {};
|
||||
|
||||
int ret = libusb_get_device_descriptor(currDev.get(), &currDevDesc);
|
||||
if (ret < 0) {
|
||||
continue;
|
||||
}
|
||||
if (((currDevDesc.idVendor & vidMask) == vid) &&
|
||||
((currDevDesc.idProduct & pidMask) == pid)) {
|
||||
matchedDevices.push_back(std::make_shared<UsbDevice>(mLibusbCtx, currDev));
|
||||
matchedDevices.push_back(std::make_unique<UsbDevice>(mLibusbCtx, std::move(currDev)));
|
||||
}
|
||||
}
|
||||
return matchedDevices;
|
||||
|
||||
Reference in New Issue
Block a user