zdev: fix memory leak in misc_readlink()

When realloc() fails to allocate memory it returns NULL and the original
block is left untouched. In that case return the potentially larger
original block.

Found using Cppcheck:
[zdev/src/misc.c:1106]: (error) Memory leak: name

Cc: Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
Reviewed-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jens Remus
2018-11-08 12:38:47 +01:00
committed by Jan Höppner
parent a4acf7c991
commit 31b77e1646

View File

@@ -1,7 +1,7 @@
/*
* zdev - Modify and display the persistent configuration of devices
*
* Copyright IBM Corp. 2016, 2017
* Copyright IBM Corp. 2016, 2018
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
@@ -1091,7 +1091,7 @@ exit_code_t misc_write_text_file_retry(const char *path, const char *text,
* at the specified path or NULL on error. */
char *misc_readlink(const char *path)
{
char *name;
char *name, *name2;
ssize_t len;
debug("Reading link %s\n", path);
@@ -1103,7 +1103,8 @@ char *misc_readlink(const char *path)
}
name[len++] = 0;
return realloc(name, len);
name2 = realloc(name, len);
return (name2) ? name2 : name;
}
/* Determine configuration set. */