Anastasia Klimchuk has uploaded this change for review.

View Change

dummyflasher.c: Extract params processing into a separate function

This makes init function easier to read.

A good side-effect of the change is: 13 error paths of params
processing are not leaking data anymore. All those error paths
return 1 back to init function which frees data.

And there was just one more error path in init function were free(data)
needed to be added.

BUG=b:185191942
TEST=ninja test

Change-Id: I04f55f77bb4703f1d88b2191c45a22be3c97bf87
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
---
M dummyflasher.c
1 file changed, 41 insertions(+), 19 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/48/54748/1
diff --git a/dummyflasher.c b/dummyflasher.c
index e19e46c..98b759e 100644
--- a/dummyflasher.c
+++ b/dummyflasher.c
@@ -654,8 +654,9 @@
return 0;
}

-int dummy_init(void)
+static int init_data(struct emu_data *data)
{
+
char *bustext = NULL;
char *tmp = NULL;
unsigned int i;
@@ -663,20 +664,6 @@
char *status = NULL;
int size = -1; /* size for VARIABLE_SIZE chip device */
#endif
-#if EMULATE_CHIP
- struct stat image_stat;
-#endif
-
- struct emu_data *data = calloc(1, sizeof(struct emu_data));
- if (!data) {
- msg_perr("Out of memory!\n");
- return 1;
- }
- data->emu_chip = EMULATE_NONE;
- data->delay_us = 0;
- par_master_dummy.data = data;
-
- msg_pspew("%s\n", __func__);

bustext = extract_programmer_param("bus");
msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
@@ -847,14 +834,15 @@
}
free(tmp);
}
-#endif
+#endif /* EMULATE_SPI_CHIP */

tmp = extract_programmer_param("emulate");
if (!tmp) {
msg_pdbg("Not emulating any flash chip.\n");
/* Nothing else to do. */
- goto dummy_init_out;
+ return 0;
}
+
#if EMULATE_SPI_CHIP
if (!strcmp(tmp, "M25P10.RES")) {
data->emu_chip = EMULATE_ST_M25P10_RES;
@@ -942,7 +930,7 @@
msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
data->emu_chip_size);
}
-#endif
+#endif /* EMULATE_SPI_CHIP */
if (data->emu_chip == EMULATE_NONE) {
msg_perr("Invalid chip specified for emulation: %s\n", tmp);
free(tmp);
@@ -987,8 +975,41 @@
msg_pdbg("Initial status register is set to 0x%02x.\n",
data->emu_status);
}
+#endif /* EMULATE_SPI_CHIP */
+#endif /* EMULATE_CHIP */
+
+ return 0;
+}
+
+int dummy_init(void)
+{
+#if EMULATE_CHIP
+ struct stat image_stat;
#endif

+ struct emu_data *data = calloc(1, sizeof(struct emu_data));
+ if (!data) {
+ msg_perr("Out of memory!\n");
+ return 1;
+ }
+ data->emu_chip = EMULATE_NONE;
+ data->delay_us = 0;
+ par_master_dummy.data = data;
+
+ msg_pspew("%s\n", __func__);
+
+ if (init_data(data)) {
+ free(data);
+ return 1;
+ }
+
+#if EMULATE_CHIP
+ if (data->emu_chip == EMULATE_NONE) {
+ msg_pdbg("Not emulating any flash chip.\n");
+ /* Nothing else to do. */
+ goto dummy_init_out;
+ }
+
msg_pdbg("Filling fake flash chip with 0x%02x, size %i\n",
data->erase_to_zero ? 0x00 : 0xff, data->emu_chip_size);
memset(flashchip_contents, data->erase_to_zero ? 0x00 : 0xff, data->emu_chip_size);
@@ -1011,13 +1032,14 @@
data->emu_persistent_image)) {
msg_perr("Unable to read %s\n", data->emu_persistent_image);
free(flashchip_contents);
+ free(data);
return 1;
}
} else {
msg_pdbg("doesn't match.\n");
}
}
-#endif
+#endif /* EMULATE_CHIP */

dummy_init_out:
if (register_shutdown(dummy_shutdown, data)) {

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

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I04f55f77bb4703f1d88b2191c45a22be3c97bf87
Gerrit-Change-Number: 54748
Gerrit-PatchSet: 1
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-MessageType: newchange