]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: Fixed incorrect return value of MatchString
authorCecil Sheng <cecil.sheng@hpe.com>
Thu, 17 Mar 2016 02:23:47 +0000 (10:23 +0800)
committerStar Zeng <star.zeng@intel.com>
Thu, 17 Mar 2016 02:53:48 +0000 (10:53 +0800)
In UEFI2.6, CapturePtr's in the Captures array returned by MatchString
are to be separatedly allocated so that they can be freed by the
caller.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Cecil Sheng <cecil.sheng@hpe.com>
Reviewed-by: Samer El-Haj-Mahmoud <elhaj@hpe.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.c

index cffbcb834af1868243bae18eb8f32261c85d6294..a5ee7d52fd17b2cb36af7cfc025dceb44becd810 100644 (file)
@@ -2,7 +2,7 @@
 \r
   EFI_REGULAR_EXPRESSION_PROTOCOL Implementation\r
 \r
-  (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
+  (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
 \r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License that accompanies this\r
@@ -91,6 +91,10 @@ OnigurumaMatch (
   CHAR8           ErrorMessage[ONIG_MAX_ERROR_MESSAGE_LEN];\r
   UINT32          Index;\r
   OnigUChar       *Start;\r
+  EFI_STATUS      Status;\r
+\r
+\r
+  Status = EFI_SUCCESS;\r
 \r
   //\r
   // Detemine the internal syntax type\r
@@ -102,7 +106,7 @@ OnigurumaMatch (
     OnigSyntax = ONIG_SYNTAX_PERL;\r
   } else {\r
     DEBUG ((DEBUG_ERROR, "Unsupported regex syntax - using default\n"));\r
-    ASSERT (FALSE);\r
+    return EFI_UNSUPPORTED;\r
   }\r
 \r
   //\r
@@ -143,6 +147,7 @@ OnigurumaMatch (
                  Region,\r
                  ONIG_OPTION_NONE\r
                  );\r
+\r
   if (OnigResult >= 0) {\r
     *Result = TRUE;\r
   } else {\r
@@ -150,6 +155,9 @@ OnigurumaMatch (
     if (OnigResult != ONIG_MISMATCH) {\r
       onig_error_code_to_str (ErrorMessage, OnigResult);\r
       DEBUG ((DEBUG_ERROR, "Regex match failed: %a\n", ErrorMessage));\r
+      onig_region_free (Region, 1);\r
+      onig_free (OnigRegex);\r
+      return EFI_DEVICE_ERROR;\r
     }\r
   }\r
 \r
@@ -158,14 +166,30 @@ OnigurumaMatch (
   //\r
   if (*Result && Captures != NULL) {\r
     *CapturesCount = Region->num_regs;\r
-    *Captures = AllocatePool (*CapturesCount * sizeof(**Captures));\r
+    *Captures = AllocateZeroPool (*CapturesCount * sizeof(**Captures));\r
     if (*Captures != NULL) {\r
       for (Index = 0; Index < *CapturesCount; ++Index) {\r
         //\r
         // Region beg/end values represent bytes, not characters\r
         //\r
-        (*Captures)[Index].CapturePtr = (CHAR16*)((UINTN)String + Region->beg[Index]);\r
         (*Captures)[Index].Length = (Region->end[Index] - Region->beg[Index]) / sizeof(CHAR16);\r
+        (*Captures)[Index].CapturePtr = AllocateCopyPool (\r
+                                          ((*Captures)[Index].Length) * sizeof (CHAR16),\r
+                                          (CHAR16*)((UINTN)String + Region->beg[Index])\r
+                                          );\r
+        if ((*Captures)[Index].CapturePtr == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
+      }\r
+\r
+      if (EFI_ERROR (Status)) {\r
+        for (Index = 0; Index < *CapturesCount; ++Index) {\r
+          if ((*Captures)[Index].CapturePtr != NULL) {\r
+            FreePool ((CHAR16*)(*Captures)[Index].CapturePtr);\r
+          }\r
+        }\r
+        FreePool (*Captures);\r
       }\r
     }\r
   }\r
@@ -173,7 +197,7 @@ OnigurumaMatch (
   onig_region_free (Region, 1);\r
   onig_free (OnigRegex);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r