diff mbox

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

Message ID 20170415110540.17036-1-xypron.glpk@gmx.de
State Accepted
Delegated to: Tom Rini
Headers show

Commit Message

Heinrich Schuchardt April 15, 2017, 11:05 a.m. UTC
If realloc fails we should release the old buffer.

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

Comments

Tom Rini April 15, 2017, 4:11 p.m. UTC | #1
On Sat, Apr 15, 2017 at 01:05:40PM +0200, Heinrich Schuchardt wrote:

> If realloc fails we should release the old buffer.
> 
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

Reviewed-by: Tom Rini <trini@konsulko.com>
Simon Glass April 16, 2017, 7:34 p.m. UTC | #2
On 15 April 2017 at 05:05, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
> If realloc fails we should release the old buffer.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
> v2:
>   Initial mail was garbled.
> ---
>  tools/env/fw_env.c | 3 +++
>  1 file changed, 3 insertions(+)

Reviewed-by: Simon Glass <sjg@chromium.org>
Tom Rini April 19, 2017, 1:03 p.m. UTC | #3
On Sat, Apr 15, 2017 at 01:05:40PM +0200, xypron.glpk@gmx.de wrote:

> If realloc fails we should release the old buffer.
> 
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> Reviewed-by: Tom Rini <trini@konsulko.com>
> Reviewed-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!
diff mbox

Patch

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;
 		}