From da3a5fdb7ba9c78f91233edb7a8f3821db2f545e Mon Sep 17 00:00:00 2001 From: Oliver Jowett Date: Wed, 30 Sep 2026 20:42:05 +0800 Subject: [PATCH] pg2-util: don't try to libusb_ref_device a null pointer if hotplug fails If hotplug detection of the new usb device after a firmware load fails, and the caller wants to know the new device, don't crash trying to call libusb_ref_device with a null pointer --- cli/pg2-util/dfu_load.c | 2 +- cli/pg2-util/mem_load.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/pg2-util/dfu_load.c b/cli/pg2-util/dfu_load.c index f8941ab..b8caa05 100644 --- a/cli/pg2-util/dfu_load.c +++ b/cli/pg2-util/dfu_load.c @@ -204,7 +204,7 @@ bool dfu_load(const firmware_image_t *image, libusb_device *dev, libusb_device * /* wait for the new device to appear */ post_hotplug = hotplug_await(hotplug_state); if (loaded_device) - *loaded_device = libusb_ref_device(post_hotplug); + *loaded_device = post_hotplug ? libusb_ref_device(post_hotplug) : NULL; cleanup: if (handle) diff --git a/cli/pg2-util/mem_load.c b/cli/pg2-util/mem_load.c index f99601f..2188fe1 100644 --- a/cli/pg2-util/mem_load.c +++ b/cli/pg2-util/mem_load.c @@ -103,7 +103,7 @@ bool mem_load(const firmware_image_t *image, libusb_device *dev, libusb_device * /* wait for the new device to appear */ post_hotplug = hotplug_await(hotplug_state); if (loaded_device) - *loaded_device = libusb_ref_device(post_hotplug); + *loaded_device = post_hotplug ? libusb_ref_device(post_hotplug) : NULL; cleanup: if (handle)