From 374871c0393bb1d67c36dc9b7280d8e5f8b22ad5 Mon Sep 17 00:00:00 2001 From: Thomas Heijligen Date: Fri, 5 Aug 2022 17:56:20 +0200 Subject: tree: Check properly if libusb is initialized The dediprog, developerbox_spi and stlinkv3_spi programmers only check if the libusb_context is not NULL after the initialization. But following the API documentation from libusb, the context is undefined unless the init function returns 0. Fix this by checking the return value instead of the libusb_context to see if the initialization was successful. https://libusb.sourceforge.io/api-1.0/group__libusb__lib.html Change-Id: Ia45ccd3fa2239dfccd821be46a09c86426cb22e5 Signed-off-by: Thomas Heijligen Reviewed-on: https://review.coreboot.org/c/flashrom/+/66460 Reviewed-by: Felix Singer Tested-by: build bot (Jenkins) --- dediprog.c | 4 ++-- developerbox_spi.c | 3 +-- stlinkv3_spi.c | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/dediprog.c b/dediprog.c index 519a8b0d..e5bf84e3 100644 --- a/dediprog.c +++ b/dediprog.c @@ -1206,8 +1206,8 @@ static int dediprog_init(void) dp_data->devicetype = DEV_UNKNOWN; /* Here comes the USB stuff. */ - libusb_init(&dp_data->usb_ctx); - if (!dp_data->usb_ctx) { + ret = libusb_init(&dp_data->usb_ctx); + if (ret) { msg_perr("Could not initialize libusb!\n"); goto init_err_exit; } diff --git a/developerbox_spi.c b/developerbox_spi.c index c84190a3..6308da63 100644 --- a/developerbox_spi.c +++ b/developerbox_spi.c @@ -147,8 +147,7 @@ static int developerbox_spi_init(void) struct libusb_context *usb_ctx; libusb_device_handle *cp210x_handle; - libusb_init(&usb_ctx); - if (!usb_ctx) { + if (libusb_init(&usb_ctx)) { msg_perr("Could not initialize libusb!\n"); return 1; } diff --git a/stlinkv3_spi.c b/stlinkv3_spi.c index 4919d48a..22a0a063 100644 --- a/stlinkv3_spi.c +++ b/stlinkv3_spi.c @@ -485,8 +485,7 @@ static int stlinkv3_spi_init(void) libusb_device_handle *stlinkv3_handle; struct stlinkv3_spi_data *stlinkv3_data; - libusb_init(&usb_ctx); - if (!usb_ctx) { + if (libusb_init(&usb_ctx)) { msg_perr("Could not initialize libusb!\n"); return 1; } -- cgit v1.2.3