[flashrom] [PATCH 2/3] Fix memleaks in cli_classic.c.

Stefan Tauner stefan.tauner at student.tuwien.ac.at
Fri Sep 14 00:38:08 CEST 2012


Frees the memory allocated for the following strings
- log file name
- layout file name
- image file name
- programmer parameter (and reset the associated global variable in flashrom.c)

Also, free the flashchip structs allocated by probe_flash.

The layout image names were not fixed due to the pending layout patches.

These bugs were found thanks to valgrind.

Signed-off-by: Stefan Tauner <stefan.tauner at student.tuwien.ac.at>
---
 cli_classic.c |   15 +++++++++++++++
 flashrom.c    |    1 +
 2 files changed, 16 insertions(+)

diff --git a/cli_classic.c b/cli_classic.c
index 58696ad..a120532 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -146,7 +146,9 @@ int main(int argc, char *argv[])
 
 	char *filename = NULL;
 	char *layoutfile = NULL;
+#ifndef STANDALONE
 	char *logfile = NULL;
+#endif /* !STANDALONE */
 	char *tempstr = NULL;
 	char *pparam = NULL;
 
@@ -235,6 +237,8 @@ int main(int argc, char *argv[])
 			tempstr = strdup(optarg);
 			if (register_include_arg(tempstr))
 				cli_classic_abort_usage();
+			/* FIXME: The image name is saved in a static array by register_include_arg() and
+			 * needs to be freed after processing them. */
 			break;
 		case 'L':
 			if (++operation_specified > 1) {
@@ -352,6 +356,7 @@ int main(int argc, char *argv[])
 		cli_classic_abort_usage();
 	if (logfile && open_logfile(logfile))
 		return 1;
+	free(logfile);
 #endif /* !STANDALONE */
 
 #if CONFIG_PRINT_WIKI == 1
@@ -478,6 +483,7 @@ int main(int argc, char *argv[])
 			}
 			msg_cinfo("Please note that forced reads most likely contain garbage.\n");
 			ret = read_flash_to_file(&flashes[0], filename);
+			free(flashes[0].chip);
 			goto out_shutdown;
 		}
 		ret = 1;
@@ -522,6 +528,15 @@ int main(int argc, char *argv[])
 out_shutdown:
 	programmer_shutdown();
 out:
+	for (i = 0; i < chipcount; i++)
+		free(flashes[i].chip);
+
+	free(filename);
+	free(layoutfile);
+	free(pparam);
+	/* clean up global variables */
+	free(chip_to_probe);
+	chip_to_probe = NULL;
 #ifndef STANDALONE
 	ret |= close_logfile();
 #endif /* !STANDALONE */
diff --git a/flashrom.c b/flashrom.c
index fdc5412..68ed652 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -351,6 +351,7 @@ int programmer_shutdown(void)
 		int i = --shutdown_fn_count;
 		ret |= shutdown_fn[i].func(shutdown_fn[i].data);
 	}
+	programmer_param = NULL;
 	return ret;
 }
 
-- 
Kind regards, Stefan Tauner





More information about the flashrom mailing list