vpelletier / python-libusb1

Python ctype-based wrapper around libusb1
GNU Lesser General Public License v2.1
168 stars 65 forks source link

Segmentation fault #58

Closed amjadtaleb closed 3 years ago

amjadtaleb commented 3 years ago

USBDevice.getBusNumber() in usb1/init.py line(1991) causes Segmentation fault if accessed after usb1.USBContext() is closed. I know it does not make much sense to access the device after closing the context manager, but it would be better if the error is caught instead of causing python process to crash.

whitequark commented 3 years ago

@vpelletier It's concerning to me that these unsafe methods with no protection from use-after-free are used in __hash__ and __eq__. Why not just memorize those particular values in __init__?

vpelletier commented 3 years ago

Why not just memorize those particular values in __init__?

I got lost trying to prevent calling these method on a closed context, but it was more overhead and more boilerplate code than I was comfortable with. But you are right, calling these in __init__ is much better, none should change during the lifetime of the device handle, and these functions should be fine to call without control from python-libusb1's user (no I/O to the device).

whitequark commented 3 years ago

Exactly.

vpelletier commented 3 years ago

Should be fixed by a1bfe7df868d87fd359d92e9cc99a904256977a8 .

whitequark commented 3 years ago

:+1: