tests/set_option: Avoid use-after-free in case of test failure

In case num_devices equals 0, LIBUSB_EXPECT() calls
LIBUSB_TEST_CLEAN_EXIT(), which calls libusb_exit() on test_ctx which
has just been freed a few lines above by a call to libusb_exit(). This
might cause a SEGFAULT or SIGBUS depending on the system.

Fixes that by assigning NULL to test_ctx just after calling
libusb_exit() like it is done elsewhere in the file.

Closes #1374
This commit is contained in:
Aurelien Jarno
2023-12-11 22:07:17 +01:00
committed by Tormod Volden
parent cc3df77609
commit ebfbf195d4
2 changed files with 2 additions and 1 deletions

View File

@@ -1 +1 @@
#define LIBUSB_NANO 11848
#define LIBUSB_NANO 11849

View File

@@ -174,6 +174,7 @@ static libusb_testlib_result test_no_discovery(void)
ssize_t num_devices = libusb_get_device_list(test_ctx, &device_list);
libusb_free_device_list(device_list, /*unref_devices=*/1);
libusb_exit(test_ctx);
test_ctx = NULL;
LIBUSB_EXPECT(>, num_devices, 0);