mirror of
https://github.com/Fishwaldo/u-boot.git
synced 2025-06-27 00:51:50 +00:00
tools/env: avoid memory leak in fw_setenv
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>
This commit is contained in:
parent
1ecd2a2f06
commit
ddc6a9de05
1 changed files with 3 additions and 0 deletions
3
tools/env/fw_env.c
vendored
3
tools/env/fw_env.c
vendored
|
@ -473,6 +473,7 @@ int fw_setenv(int argc, char *argv[], struct env_opts *opts)
|
||||||
int i;
|
int i;
|
||||||
size_t len;
|
size_t len;
|
||||||
char *name, **valv;
|
char *name, **valv;
|
||||||
|
char *oldval;
|
||||||
char *value = NULL;
|
char *value = NULL;
|
||||||
int valc;
|
int valc;
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -507,11 +508,13 @@ int fw_setenv(int argc, char *argv[], struct env_opts *opts)
|
||||||
|
|
||||||
if (value)
|
if (value)
|
||||||
value[len - 1] = ' ';
|
value[len - 1] = ' ';
|
||||||
|
oldval = value;
|
||||||
value = realloc(value, len + val_len + 1);
|
value = realloc(value, len + val_len + 1);
|
||||||
if (!value) {
|
if (!value) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Cannot malloc %zu bytes: %s\n",
|
"Cannot malloc %zu bytes: %s\n",
|
||||||
len, strerror(errno));
|
len, strerror(errno));
|
||||||
|
free(oldval);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue