>From d3f69cab3b86c313f178e33f3ed51ad47c366ed7 Mon Sep 17 00:00:00 2001
From: Tadas Slotkus <devtadas@gmail.com>
Date: Sat, 20 Aug 2011 21:07:09 +0300
Subject: [carFl mem 49/65] extract_programmer_param: use buf from globals

extract_programmer_param now uses statically allocated buffer for
temporary strings.

Signed-off-by: Tadas Slotkus <devtadas@gmail.com>
---
 chipset_enable.c |    4 ++--
 dummyflasher.c   |    6 +++---
 flash.h          |    3 +++
 flashrom.c       |    4 ++--
 internal.c       |   18 +++++++++---------
 it87spi.c        |    4 ++--
 6 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/chipset_enable.c b/chipset_enable.c
index 2fc255b..05dfcf1 100644
--- a/chipset_enable.c
+++ b/chipset_enable.c
@@ -341,13 +341,13 @@ static int enable_flash_ich_dc(globals *g, struct pci_dev *dev, const char *name
 	} else if (idsel) {
 		msg_perr("Error: fwh_idsel= specified, but no value given.\n");
 idsel_garbage_out:	
-		free(idsel);
+		//free(idsel);
 		/* FIXME: Return failure here once internal_init() starts
 		 * to care about the return value of the chipset enable.
 		 */
 		exit(1);
 	}
-	free(idsel);
+	//free(idsel);
 
 	/* Ignore all legacy ranges below 1 MB.
 	 * We currently only support flashing the chip which responds to
diff --git a/dummyflasher.c b/dummyflasher.c
index 5e60259..05e389b 100644
--- a/dummyflasher.c
+++ b/dummyflasher.c
@@ -127,12 +127,12 @@ int dummy_init(globals *g)
 	}
 	if (buses_supported == BUS_NONE)
 		msg_pdbg("Support for all flash bus types disabled.\n");
-	free(bustext);
+	//free(bustext);
 
 	tmp = extract_programmer_param(g, "spi_write_256_chunksize");
 	if (tmp) {
 		spi_write_256_chunksize = atoi(tmp);
-		free(tmp);
+		//free(tmp);
 		if (spi_write_256_chunksize < 1) {
 			msg_perr("invalid spi_write_256_chunksize\n");
 			return 1;
@@ -192,7 +192,7 @@ int dummy_init(globals *g)
 		free(tmp);
 		return 1;
 	}
-	free(tmp);
+	//free(tmp);
 	flashchip_contents = malloc(emu_chip_size);
 	if (!flashchip_contents) {
 		msg_perr("Out of memory!\n");
diff --git a/flash.h b/flash.h
index b98f40b..bb97df0 100644
--- a/flash.h
+++ b/flash.h
@@ -289,10 +289,13 @@ enum programmer {
 	PROGRAMMER_INVALID /* This must always be the last entry. */
 };
 
+#define TMP_BUF_SIZE 60
+
 struct globals {
 	enum programmer programmer;
 	uint8_t undo_data_buf[256];
 	uint8_t *undo_data_free_ptr; //pointer to free space (for new undo data)
+	char tmp_buf[TMP_BUF_SIZE]; // for extract programmer param
 };
 void init_globals(globals *g);
 
diff --git a/flashrom.c b/flashrom.c
index 7a0b238..2dd1468 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -639,10 +639,10 @@ char *extract_param(globals *g, char **haystack, const char *needle, const char
 		opt_pos = param_pos + needlelen + 1;
 		optlen = strcspn(opt_pos, delim);
 		/* Return an empty string if the parameter was empty. */
-		opt = malloc(optlen + 1);
+		opt = g->tmp_buf;
 		if (!opt) {
 			msg_gerr("Out of memory!\n");
-			exit(1);
+			return 0;
 		}
 		strncpy(opt, opt_pos, optlen);
 		opt[optlen] = '\0';
diff --git a/internal.c b/internal.c
index d237bce..2301181 100644
--- a/internal.c
+++ b/internal.c
@@ -146,42 +146,42 @@ int internal_init(globals *g)
 		force_boardenable = 1;
 	} else if (arg && !strlen(arg)) {
 		msg_perr("Missing argument for boardenable.\n");
-		free(arg);
+//		free(arg);
 		return 1;
 	} else if (arg) {
 		msg_perr("Unknown argument for boardenable: %s\n", arg);
-		free(arg);
+//		free(arg);
 		return 1;
 	}
-	free(arg);
+//	free(arg);
 
 	arg = extract_programmer_param(g, "boardmismatch");
 	if (arg && !strcmp(arg,"force")) {
 		force_boardmismatch = 1;
 	} else if (arg && !strlen(arg)) {
 		msg_perr("Missing argument for boardmismatch.\n");
-		free(arg);
+//		free(arg);
 		return 1;
 	} else if (arg) {
 		msg_perr("Unknown argument for boardmismatch: %s\n", arg);
-		free(arg);
+//		free(arg);
 		return 1;
 	}
-	free(arg);
+//	free(arg);
 
 	arg = extract_programmer_param(g, "laptop");
 	if (arg && !strcmp(arg,"force_I_want_a_brick")) {
 		force_laptop = 1;
 	} else if (arg && !strlen(arg)) {
 		msg_perr("Missing argument for laptop.\n");
-		free(arg);
+//		free(arg);
 		return 1;
 	} else if (arg) {
 		msg_perr("Unknown argument for laptop: %s\n", arg);
-		free(arg);
+//		free(arg);
 		return 1;
 	}
-	free(arg);
+//	free(arg);
 
 	get_io_perms();
 	if (register_shutdown(g, internal_shutdown, NULL))
diff --git a/it87spi.c b/it87spi.c
index ffcf8ce..7f0bf4c 100644
--- a/it87spi.c
+++ b/it87spi.c
@@ -181,7 +181,7 @@ static uint16_t it87spi_probe(globals *g, uint16_t port)
 			msg_perr("Error: it87spiport specified, but no valid "
 				 "port specified.\nPort must be a multiple of "
 				 "0x8 and lie between 0x100 and 0xff8.\n");
-			free(portpos);
+//			free(portpos);
 			return 1;
 		} else {
 			flashport = (uint16_t)forced_flashport;
@@ -191,7 +191,7 @@ static uint16_t it87spi_probe(globals *g, uint16_t port)
 			sio_write(port, 0x65, (flashport & 0xff));
 		}
 	}
-	free(portpos);
+//	free(portpos);
 	exit_conf_mode_ite(port);
 	it8716f_flashport = flashport;
 	if (buses_supported & BUS_SPI)
-- 
1.7.6

