ft2232_spi: Fix resource leaking on failed init
Move the allocation of `spi_data` down. OOM errors are less likely,
hence it won't hurt to check that late. OTOH, it simplifies error
handling when we separate init parts that require dynamic resources
from those that don't.
Change-Id: Ibfc35832e71eb05571801216763af238c1dfd1eb
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/73575
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/ft2232_spi.c b/ft2232_spi.c
index 4784f46..246956e 100644
--- a/ft2232_spi.c
+++ b/ft2232_spi.c
@@ -333,12 +333,6 @@
uint8_t aux_bits_high = 0x00;
uint8_t pindir_high = 0x00;
- struct ft2232_data *const spi_data = calloc(1, sizeof(*spi_data));
- if (!spi_data) {
- msg_perr("Unable to allocate space for SPI master data\n");
- return SPI_GENERIC_ERROR;
- }
-
arg = extract_programmer_param("type");
if (arg) {
if (!strcasecmp(arg, "2232H")) {
@@ -438,8 +432,7 @@
} else {
msg_perr("Error: Invalid device type specified.\n");
free(arg);
- ret = -1;
- goto init_err;
+ return -1;
}
}
free(arg);
@@ -475,8 +468,7 @@
if (channel_count < 0 || strlen(arg) != 1) {
msg_perr("Error: Invalid channel/port/interface specified: \"%s\".\n", arg);
free(arg);
- ret = -2;
- goto init_err;
+ return -2;
}
}
free(arg);
@@ -490,8 +482,7 @@
msg_perr("Error: Invalid SPI frequency divisor specified: \"%s\".\n"
"Valid are even values between 2 and 131072.\n", arg);
free(arg);
- ret = -2;
- goto init_err;
+ return -2;
}
divisor = (uint32_t)temp;
}
@@ -510,8 +501,7 @@
msg_perr("Error: Invalid GPIOL specified: \"%s\".\n"
"Valid values are between 0 and 3.\n", arg);
free(arg);
- ret = -2;
- goto init_err;
+ return -2;
}
unsigned int pin = temp + 4;
@@ -597,6 +587,12 @@
(ft2232_interface == INTERFACE_B) ? "B" :
(ft2232_interface == INTERFACE_C) ? "C" : "D");
+ struct ft2232_data *const spi_data = calloc(1, sizeof(*spi_data));
+ if (!spi_data) {
+ msg_perr("Unable to allocate space for SPI master data\n");
+ return SPI_GENERIC_ERROR;
+ }
+
struct ftdi_context *const ftdic = &spi_data->ftdi_context;
if (ftdi_init(ftdic) < 0) {
msg_perr("ftdi_init failed.\n");