From 3244f2e92a0726a22e99218e0535879038c11d85 Mon Sep 17 00:00:00 2001 From: Gwenhael Goavec-Merou Date: Thu, 23 Dec 2021 10:29:47 +0100 Subject: [PATCH] dfu: fix memory leak --- src/dfu.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/dfu.cpp b/src/dfu.cpp index 61fe90a..c6f1e9c 100644 --- a/src/dfu.cpp +++ b/src/dfu.cpp @@ -98,6 +98,7 @@ DFU::DFU(const string &filename, uint16_t vid, uint16_t pid, if ((dfu_vid == 0 || dfu_pid == 0) && (vid == 0 || pid == 0)) { // search all DFU compatible devices if (searchDFUDevices() != EXIT_SUCCESS) { + libusb_exit(usb_ctx); delete _bit; throw std::runtime_error("Devices enumeration failed"); } @@ -112,9 +113,14 @@ DFU::DFU(const string &filename, uint16_t vid, uint16_t pid, /* check if DFU compatible devices are present */ if (dfu_dev.size() != 0) { /* more than one: only possible if file is not DFU */ - if (dfu_dev.size() > 1 && !filename.empty()) + if (dfu_dev.size() > 1 && !filename.empty()) { + libusb_exit(usb_ctx); + delete _bit; throw std::runtime_error("Only one device supported"); + } } else { + libusb_exit(usb_ctx); + delete _bit; throw std::runtime_error("No DFU compatible device found"); } @@ -123,11 +129,14 @@ DFU::DFU(const string &filename, uint16_t vid, uint16_t pid, /* don't try device without vid/pid */ if (_vid == 0 || _pid == 0) { + libusb_exit(usb_ctx); + delete _bit; throw std::runtime_error("Can't open device vid/pid == 0"); } /* open the first */ if (open_DFU(0) == EXIT_FAILURE) { + libusb_exit(usb_ctx); delete _bit; throw std::runtime_error("Fail to claim device"); }