From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A5F8D81CCC for ; Wed, 9 Nov 2016 20:22:23 -0800 (PST) Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga105.fm.intel.com with ESMTP; 09 Nov 2016 20:22:27 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,616,1473145200"; d="scan'208";a="784684319" Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.34]) by FMSMGA003.fm.intel.com with ESMTP; 09 Nov 2016 20:22:26 -0800 From: Hao Wu To: edk2-devel@lists.01.org Cc: Hao Wu , Liming Gao , Yonghong Zhu Date: Thu, 10 Nov 2016 12:22:17 +0800 Message-Id: <1478751738-16372-2-git-send-email-hao.a.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.0 In-Reply-To: <1478751738-16372-1-git-send-email-hao.a.wu@intel.com> References: <1478751738-16372-1-git-send-email-hao.a.wu@intel.com> Subject: [PATCH 1/2] BaseTools/GenFfs: Fix return too early when input file is of size 0 X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Nov 2016 04:22:23 -0000 Commit 2cb874352423fcfd180199e6de8298567dff8e7f eliminates possible NULL pointer dereference in GenFfs tool source codes. However, it doesn't correctly handle the case when the input file is of size 0. This will lead to possible build issues. This commits refine the logic to handle the above case. Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu --- BaseTools/Source/C/GenFfs/GenFfs.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/BaseTools/Source/C/GenFfs/GenFfs.c b/BaseTools/Source/C/GenFfs/GenFfs.c index 78e5097..c5d657b 100644 --- a/BaseTools/Source/C/GenFfs/GenFfs.c +++ b/BaseTools/Source/C/GenFfs/GenFfs.c @@ -842,7 +842,12 @@ Returns: ); } - if (EFI_ERROR (Status) || (FileBuffer == NULL)) { + if (EFI_ERROR (Status)) { + goto Finish; + } + + if (FileBuffer == NULL && FileSize != 0) { + Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!"); goto Finish; } @@ -929,7 +934,9 @@ Returns: // // write data // - fwrite (FileBuffer, 1, FileSize - HeaderSize, FfsFile); + if (FileBuffer != NULL) { + fwrite (FileBuffer, 1, FileSize - HeaderSize, FfsFile); + } fclose (FfsFile); } -- 1.9.5.msysgit.0