stlinkv3_spi.c: Clean up properly on all init error paths
If register_spi_master() fails, going to init exit cleanup is not
needed because at that point shutdown function has already been
registered and it does the job.
Change-Id: I9fabf48068635593bc86006c9642d8569eee8447
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/54190
Original-Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Original-Reviewed-by: Miklós Márton <martonmiklosqdev@gmail.com>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/71356
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/stlinkv3_spi.c b/stlinkv3_spi.c
index 7e8336e..fbb9b1b 100644
--- a/stlinkv3_spi.c
+++ b/stlinkv3_spi.c
@@ -464,6 +464,7 @@
char *speed_str = NULL;
char *serialno = NULL;
char *endptr = NULL;
+ int ret = 1;
libusb_init(&usb_ctx);
if (!usb_ctx) {
@@ -485,7 +486,7 @@
else
msg_perr("Could not find any connected STLINK-V3\n");
free(serialno);
- goto err_exit;
+ goto init_err_exit;
}
free(serialno);
@@ -498,23 +499,30 @@
msg_perr("Please pass the parameter "
"with a simple non-zero number in kHz\n");
free(speed_str);
- return -1;
+ ret = -1;
+ goto init_err_exit;
}
free(speed_str);
}
if (stlinkv3_spi_open(sck_freq_kHz))
- goto err_exit;
+ goto init_err_exit;
if (register_shutdown(stlinkv3_spi_shutdown, NULL))
- goto err_exit;
+ goto init_err_cleanup_exit;
if (register_spi_master(&spi_programmer_stlinkv3))
- goto err_exit;
+ return 1; /* shutdown function does cleanup */
return 0;
-err_exit:
- libusb_exit(usb_ctx);
+init_err_cleanup_exit:
+ stlinkv3_spi_shutdown(NULL);
return 1;
+
+init_err_exit:
+ if (stlinkv3_handle)
+ libusb_close(stlinkv3_handle);
+ libusb_exit(usb_ctx);
+ return ret;
}