MiSo1289 / usb-asio

Libusb wrapper for Asio
MIT License
63 stars 5 forks source link

Segfault (nullptr) access in interface claim #1

Closed diggit closed 1 year ago

diggit commented 1 year ago

Hi, first of all, thank you for all your work... díky :)

I am trying to use your asio wrapper of libusb and I am struggling a little bit. It's all blowing up in interface claiming. There is uninitialized data access resulting in null pointer de-reference.

My code is just slightly modified example from readme.

Constructing interface object calls claim.

claim itself calls unclaim at first.

unclaim calls ::libusb_release_interface which takes device handler as argument.

Device handle pointer is obtained by calling device_handle, which returns device_handle_, but this data member is not set yet. That happens at the end of claim fcn.

Unless I am using it wrong, there is several ways to fix this:

MiSo1289 commented 1 year ago

Hi, thanks for the report; unclaim() should be a no-op if handle is not set, but it looks like is_claimed() returns the wrong thing:

        [[nodiscard]] auto is_claimed() const noexcept -> bool
        {
            return device_handle_ == nullptr;
        }

Should be != nullptr, will fix.

I planned to use this library for a project which I had to abandon, so there are definitely bugs in basic features like this; I might get to use it for another project in not-so-distant future though :) feel free to report other problems / submit PRs if you find other issues.

diggit commented 1 year ago

Should be != nullptr, will fix.

Oh, I totally missed that. Thanks for quick fix!

diggit commented 1 year ago

I believe, this is not fully fixed yet, as basic_usb_interface::device_handle_ is missing initialization and thus is_claimed() may incorrectly return because device_handle_ holds some garbage.