[PATCH] uefi: uefidump: free original string on failed realloc

Colin King colin.king at canonical.com
Thu Apr 13 10:08:34 UTC 2017


From: Colin Ian King <colin.king at canonical.com>

A common bug is where realloc fails to allocate and we assume that
the memory being realloc'd was freed. This is not the case, the
NULL return means we need to free the original string to avoid
a memory leak.

Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
 src/uefi/uefidump/uefidump.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/uefi/uefidump/uefidump.c b/src/uefi/uefidump/uefidump.c
index 90556204..305d2d6a 100644
--- a/src/uefi/uefidump/uefidump.c
+++ b/src/uefi/uefidump/uefidump.c
@@ -83,9 +83,13 @@ static char *uefidump_vprintf(char *str, const char *fmt, ...)
 	if (str == NULL)
 		str = strdup(buffer);
 	else {
-		str = realloc(str, strlen(str) + strlen(buffer) + 1);
-		if (str == NULL)
+		char *tmp;
+		tmp = realloc(str, strlen(str) + strlen(buffer) + 1);
+		if (!tmp) {
+			free(str);
 			return NULL;
+		}
+		str = tmp;
 		strcat(str, buffer);
 	}
 
-- 
2.11.0




More information about the fwts-devel mailing list