From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (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 63F681A1E4F for ; Thu, 8 Sep 2016 09:14:24 -0700 (PDT) Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C41D7C030713; Thu, 8 Sep 2016 16:14:23 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-57.phx2.redhat.com [10.3.116.57]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u88GEIF5030512; Thu, 8 Sep 2016 12:14:22 -0400 From: Laszlo Ersek To: edk2-devel@ml01.01.org Cc: Jaben Carsey , Ruiyu Ni , Tapan Shah Date: Thu, 8 Sep 2016 18:14:14 +0200 Message-Id: <20160908161414.3143-3-lersek@redhat.com> In-Reply-To: <20160908161414.3143-1-lersek@redhat.com> References: <20160908161414.3143-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 08 Sep 2016 16:14:23 +0000 (UTC) Subject: [PATCH 2/2] ShellPkg/UefiHandleParsingLib: fix retval for empty child controller array 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, 08 Sep 2016 16:14:24 -0000 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 Cc: Ruiyu Ni Cc: Tapan Shah Reported-by: Tapan Shah Ref: https://tianocore.acgmultimedia.com/show_bug.cgi?id=112 Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek --- ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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); } -- 2.9.2