From b390737ad03a354f5d11954950a580df715e2935 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Wed, 9 Nov 2016 23:00:01 +0800 Subject: BaseTools/GenFfs: Fix return too early when input file is of size 0 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 Reviewed-by: Liming Gao --- BaseTools/Source/C/GenFfs/GenFfs.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'BaseTools') diff --git a/BaseTools/Source/C/GenFfs/GenFfs.c b/BaseTools/Source/C/GenFfs/GenFfs.c index 78e5097fb4..c5d657bd09 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); } -- cgit v1.2.3