]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/Core/Dxe: assert SectionInstance invariant in FindChildNode()
authorLaszlo Ersek <lersek@redhat.com>
Thu, 19 Nov 2020 10:53:39 +0000 (11:53 +0100)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sat, 21 Nov 2020 01:40:53 +0000 (01:40 +0000)
FindChildNode() has two callers: GetSection(), and FindChildNode() itself.

- At the GetSection() call site, a positive (i.e., nonzero)
  SectionInstance is passed. This is because GetSection() takes a
  zero-based (UINTN) SectionInstance, and then passes
  Instance=(SectionInstance+1) to FindChildNode().

- For reaching the recursive FindChildNode() call site, a section type
  mismatch, or a section instance mismatch, is necessary. This means,
  respectively, that SectionInstance will either not have been decreased,
  or not to zero anyway, at the recursive FindChildNode() call site.

Add two ASSERT()s to FindChildNode(), for expressing the (SectionSize>0)
invariant.

In turn, the invariant provides the explanation why, after the recursive
call, a zero SectionInstance implies success. Capture it in a comment.

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20201119105340.16225-2-lersek@redhat.com>

MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c

index d678166db475423d56154c6d1c57b23a8cafc17e..d7f7ef427422cb7044fc775dac4eec5bd7341e1a 100644 (file)
@@ -952,8 +952,8 @@ CreateChildNode (
                                  search.\r
   @param  SearchType             Indicates the type of section to search for.\r
   @param  SectionInstance        Indicates which instance of section to find.\r
-                                 This is an in/out parameter to deal with\r
-                                 recursions.\r
+                                 This is an in/out parameter and it is 1-based,\r
+                                 to deal with recursions.\r
   @param  SectionDefinitionGuid  Guid of section definition\r
   @param  FoundChild             Output indicating the child node that is found.\r
   @param  FoundStream            Output indicating which section stream the child\r
@@ -988,6 +988,8 @@ FindChildNode (
   EFI_STATUS                                    ErrorStatus;\r
   EFI_STATUS                                    Status;\r
 \r
+  ASSERT (*SectionInstance > 0);\r
+\r
   CurrentChildNode = NULL;\r
   ErrorStatus = EFI_NOT_FOUND;\r
 \r
@@ -1037,6 +1039,11 @@ FindChildNode (
       }\r
     }\r
 \r
+    //\r
+    // Type mismatch, or we haven't found the desired instance yet.\r
+    //\r
+    ASSERT (*SectionInstance > 0);\r
+\r
     if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
       //\r
       // If the current node is an encapsulating node, recurse into it...\r
@@ -1050,16 +1057,20 @@ FindChildNode (
                 &RecursedFoundStream,\r
                 AuthenticationStatus\r
                 );\r
-      //\r
-      // If the status is not EFI_SUCCESS, just save the error code and continue\r
-      // to find the request child node in the rest stream.\r
-      //\r
       if (*SectionInstance == 0) {\r
+        //\r
+        // The recursive FindChildNode() call decreased (*SectionInstance) to\r
+        // zero.\r
+        //\r
         ASSERT_EFI_ERROR (Status);\r
         *FoundChild = RecursedChildNode;\r
         *FoundStream = RecursedFoundStream;\r
         return EFI_SUCCESS;\r
       } else {\r
+        //\r
+        // If the status is not EFI_SUCCESS, just save the error code and\r
+        // continue to find the request child node in the rest stream.\r
+        //\r
         ErrorStatus = Status;\r
       }\r
     } else if ((CurrentChildNode->Type == EFI_SECTION_GUID_DEFINED) && (SearchType != EFI_SECTION_GUID_DEFINED)) {\r