From 7c21beca63e7d4b07d7ebff25428ca4cfdbc8ec5 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Sun, 23 Dec 2012 17:53:25 +0100 Subject: [PATCH] C++ wrapper: Fix use-after-free issue in List::find_all() We can no longer call ftdi_deinit() inside find_all(). This will call libusb_exit() which invalidates all further operations on device lists / usb devices pointers. Instead we pass in the Ftdi::Context from the calling side. --- examples/find_all_pp.cpp | 3 ++- ftdipp/ftdi.cpp | 7 ++----- ftdipp/ftdi.hpp | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/examples/find_all_pp.cpp b/examples/find_all_pp.cpp index cf1675b..4061cd9 100644 --- a/examples/find_all_pp.cpp +++ b/examples/find_all_pp.cpp @@ -45,7 +45,8 @@ int main(int argc, char **argv) << std::endl << std::dec; // Print whole list - List* list = List::find_all(vid, pid); + Context context; + List* list = List::find_all(context, vid, pid); for (List::iterator it = list->begin(); it != list->end(); it++) { std::cout << "FTDI (" << &*it << "): " diff --git a/ftdipp/ftdi.cpp b/ftdipp/ftdi.cpp index e2755d8..95f62a0 100644 --- a/ftdipp/ftdi.cpp +++ b/ftdipp/ftdi.cpp @@ -619,13 +619,10 @@ List::iterator List::erase(iterator beg, iterator end) return d->list.erase(beg, end); } -List* List::find_all(int vendor, int product) +List* List::find_all(Context &context, int vendor, int product) { struct ftdi_device_list* dlist = 0; - struct ftdi_context ftdi; - ftdi_init(&ftdi); - ftdi_usb_find_all(&ftdi, &dlist, vendor, product); - ftdi_deinit(&ftdi); + ftdi_usb_find_all(context.context(), &dlist, vendor, product); return new List(dlist); } diff --git a/ftdipp/ftdi.hpp b/ftdipp/ftdi.hpp index ec0e13d..d853717 100644 --- a/ftdipp/ftdi.hpp +++ b/ftdipp/ftdi.hpp @@ -174,7 +174,7 @@ public: List(struct ftdi_device_list* devlist = 0); ~List(); - static List* find_all(int vendor, int product); + static List* find_all(Context &context, int vendor, int product); /// List type storing "Context" objects typedef std::list ListType; -- 1.7.1