From 31b77e16469cfcdd8c55e9443f4d8c2cd7f93f9e Mon Sep 17 00:00:00 2001 From: Jens Remus Date: Thu, 8 Nov 2018 12:38:47 +0100 Subject: [PATCH] zdev: fix memory leak in misc_readlink() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Jens Remus Reviewed-by: Peter Oberparleiter Signed-off-by: Jan Höppner --- zdev/src/misc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/zdev/src/misc.c b/zdev/src/misc.c index fe14c6c9..da4999f3 100644 --- a/zdev/src/misc.c +++ b/zdev/src/misc.c @@ -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. */