[U-Boot] [PATCH 1/1] tools/env: avoid memory leak in fw_setenv

Heinrich at lists.denx.de Heinrich at lists.denx.de
Sat Apr 15 10:56:43 UTC 2017


From: Heinrich Schuchardt <xypron.glpk at gmx.de>

If realloc fails we should release the old buffer.

Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
---
 tools/env/fw_env.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 862a0b1a02..31c18d73bc 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -469,6 +469,7 @@ int fw_setenv(int argc, char *argv[], struct env_opts *opts)
 	int i;
 	size_t len;
 	char *name, **valv;
+	char *oldval;
 	char *value = NULL;
 	int valc;
 
@@ -500,11 +501,13 @@ int fw_setenv(int argc, char *argv[], struct env_opts *opts)
 
 		if (value)
 			value[len - 1] = ' ';
+		oldval = value;
 		value = realloc(value, len + val_len + 1);
 		if (!value) {
 			fprintf(stderr,
 				"Cannot malloc %zu bytes: %s\n",
 				len, strerror(errno));
+			free(oldval);
 			return -1;
 		}
 
-- 
2.11.0



More information about the U-Boot mailing list