diff mbox

[edk2,2/2] ShellPkg/UefiHandleParsingLib: fix retval for empty child controller array

Message ID 20160908161414.3143-3-lersek@redhat.com
State Accepted
Commit 7eb3bb6c552d59b28f07ce5787049b53da76d5cf
Headers show

Commit Message

Laszlo Ersek Sept. 8, 2016, 4:14 p.m. UTC
The ParseHandleDatabaseForChildControllers() function intends to work like
this:

(1) It allocates a "HandleBufferForReturn" local array that's guaranteed
    to be big enough for all found handles,

(2) it collects the handles, both counting them in the (mandatory)
    "MatchingHandleCount" output parameter, and saving them in the local
    "HandleBufferForReturn" array,

(3) if the caller is not interested in the actual handles, then
    "HandleBufferForReturn" is released,

(4) if the caller is interested in the handles, and we've found some, then
    "HandleBufferForReturn" is passed out through the
    "MatchingHandleBuffer" output parameter,

(5) if the caller is interested in the actual handles, but we've found
    none, then the "MatchingHandleBuffer" output parameter is set to NULL.

The ASSERT() at the end of the function makes this clear, but the
implementation does not conform to (5). Fix it.

Cc: Jaben Carsey <jaben.carsey@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Tapan Shah <tapandshah@hpe.com>
Reported-by: Tapan Shah <tapandshah@hpe.com>
Ref: https://tianocore.acgmultimedia.com/show_bug.cgi?id=112
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>

---
 ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

-- 
2.9.2

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

Patch

diff --git a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c
index e11a3ccceab3..695d090926e1 100644
--- a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c
+++ b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c
@@ -2799,17 +2799,24 @@  ParseHandleDatabaseForChildControllers(
 
     FreePool (ChildControllerHandleBuffer);
   }
 
   FreePool (DriverBindingHandleBuffer);
 
+  if (MatchingHandleBuffer == NULL || *MatchingHandleCount == 0) {
+    //
+    // The caller is not interested in the actual handles, or we've found none.
+    //
+    FreePool (HandleBufferForReturn);
+    HandleBufferForReturn = NULL;
+  }
+
   if (MatchingHandleBuffer != NULL) {
     *MatchingHandleBuffer = HandleBufferForReturn;
-  } else {
-    FreePool(HandleBufferForReturn);
   }
+
   ASSERT ((MatchingHandleBuffer == NULL) ||
           (*MatchingHandleCount == 0 && *MatchingHandleBuffer == NULL) ||
           (*MatchingHandleCount != 0 && *MatchingHandleBuffer != NULL));
 
   return (EFI_SUCCESS);
 }