From patchwork Thu Apr 13 10:08:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 750392 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) by ozlabs.org (Postfix) with ESMTP id 3w3c211fWrz9s87; Thu, 13 Apr 2017 20:08:41 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1cybg8-0001eJ-1u; Thu, 13 Apr 2017 10:08:40 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.76) (envelope-from ) id 1cybg2-0001e9-UT for fwts-devel@lists.ubuntu.com; Thu, 13 Apr 2017 10:08:34 +0000 Received: from 1.general.cking.uk.vpn ([10.172.193.212] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.76) (envelope-from ) id 1cybg2-0001ZB-JO; Thu, 13 Apr 2017 10:08:34 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH] uefi: uefidump: free original string on failed realloc Date: Thu, 13 Apr 2017 11:08:34 +0100 Message-Id: <20170413100834.7458-1-colin.king@canonical.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.14 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: fwts-devel-bounces@lists.ubuntu.com From: Colin Ian King 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 Acked-by: Alex Hung Acked-by: Ivan Hu --- 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); }