C++ wrapper: Fix use-after-free issue in List::find_all()
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Sun, 23 Dec 2012 16:53:25 +0000 (17:53 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Sun, 23 Dec 2012 16:57:11 +0000 (17:57 +0100)
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
ftdipp/ftdi.cpp
ftdipp/ftdi.hpp

index cf1675b..4061cd9 100644 (file)
@@ -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 << "): "
index e2755d8..95f62a0 100644 (file)
@@ -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);
 }
 
index ec0e13d..d853717 100644 (file)
@@ -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<Context> ListType;