From cee470ecefbfdf7a1d08cb9d5cf3824c0f76e5c5 Mon Sep 17 00:00:00 2001 From: Anastasia Klimchuk Date: Fri, 14 May 2021 14:01:34 +1000 Subject: stlinkv3_spi.c: Clean up properly on all init error paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. BUG=b:185191942 TEST=builds Change-Id: I9fabf48068635593bc86006c9642d8569eee8447 Signed-off-by: Anastasia Klimchuk Reviewed-on: https://review.coreboot.org/c/flashrom/+/54190 Tested-by: build bot (Jenkins) Reviewed-by: Edward O'Callaghan Reviewed-by: Miklós Márton --- stlinkv3_spi.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/stlinkv3_spi.c b/stlinkv3_spi.c index 7712ec21..100a94b2 100644 --- a/stlinkv3_spi.c +++ b/stlinkv3_spi.c @@ -464,6 +464,7 @@ int stlinkv3_spi_init(void) char *speed_str = NULL; char *serialno = NULL; char *endptr = NULL; + int ret = 1; libusb_init(&usb_ctx); if (!usb_ctx) { @@ -485,7 +486,7 @@ int stlinkv3_spi_init(void) 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 @@ int stlinkv3_spi_init(void) 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, NULL)) - 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; } -- cgit v1.2.3