Edward O'Callaghan submitted this change.

View Change

Approvals: build bot (Jenkins): Verified Miklós Márton: Looks good to me, but someone else must approve Edward O'Callaghan: Looks good to me, approved
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.

BUG=b:185191942
TEST=builds

Change-Id: I9fabf48068635593bc86006c9642d8569eee8447
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/54190
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-by: Miklós Márton <martonmiklosqdev@gmail.com>
---
M stlinkv3_spi.c
1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/stlinkv3_spi.c b/stlinkv3_spi.c
index 7712ec2..100a94b 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, 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;
}

To view, visit change 54190. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I9fabf48068635593bc86006c9642d8569eee8447
Gerrit-Change-Number: 54190
Gerrit-PatchSet: 4
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-Reviewer: Angel Pons <th3fanbus@gmail.com>
Gerrit-Reviewer: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-Reviewer: Miklós Márton <martonmiklosqdev@gmail.com>
Gerrit-Reviewer: Nico Huber <nico.h@gmx.de>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-MessageType: merged