[edk2] [PATCH 3/5] BaseTools/DevicePath: Fix potential memory leak

Hao Wu posted 5 patches 6 years, 11 months ago
[edk2] [PATCH 3/5] BaseTools/DevicePath: Fix potential memory leak
Posted by Hao Wu 6 years, 11 months ago
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
---
 BaseTools/Source/C/DevicePath/DevicePath.c         |  1 +
 BaseTools/Source/C/DevicePath/DevicePathFromText.c | 11 +++++++++++
 2 files changed, 12 insertions(+)

diff --git a/BaseTools/Source/C/DevicePath/DevicePath.c b/BaseTools/Source/C/DevicePath/DevicePath.c
index 76b8553b71..4f859a0e44 100644
--- a/BaseTools/Source/C/DevicePath/DevicePath.c
+++ b/BaseTools/Source/C/DevicePath/DevicePath.c
@@ -190,5 +190,6 @@ int main(int argc, CHAR8 *argv[])
   }
   PrintMem (DevicePath, DevicePath->Length[0] | DevicePath->Length[1] << 8);
   putchar('\n');
+  free(Str16);
   return STATUS_SUCCESS;
 }
diff --git a/BaseTools/Source/C/DevicePath/DevicePathFromText.c b/BaseTools/Source/C/DevicePath/DevicePathFromText.c
index 3d2f5a811c..120f2a38a2 100644
--- a/BaseTools/Source/C/DevicePath/DevicePathFromText.c
+++ b/BaseTools/Source/C/DevicePath/DevicePathFromText.c
@@ -3277,6 +3277,17 @@ UefiDevicePathLibConvertTextToDeviceNode (
     //
     FromText = DevPathFromTextFilePath;
     DeviceNode = FromText (DeviceNodeStr);
+    //
+    // According to above logic, if 'FromText' is NULL in the 'if' statement,
+    // then 'ParamStr' must be NULL as well. No memory allocation has been made
+    // in this case.
+    //
+    // The below check is for addressing a false positive potential memory leak
+    // issue raised from static analysis.
+    //
+    if (ParamStr != NULL) {
+      free (ParamStr);
+    }
   } else {
     DeviceNode = FromText (ParamStr);
     free (ParamStr);
-- 
2.12.0.windows.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel