]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.c
MdeModulePkg/BdsDxe: Avoid overwriting PlatformRecovery####
[mirror_edk2.git] / MdeModulePkg / Universal / RegularExpressionDxe / RegularExpressionDxe.c
index 658031675e335c98bd5f5cf98262f5284d0d9c89..a5ee7d52fd17b2cb36af7cfc025dceb44becd810 100644 (file)
@@ -2,7 +2,7 @@
 \r
   EFI_REGULAR_EXPRESSION_PROTOCOL Implementation\r
 \r
 \r
   EFI_REGULAR_EXPRESSION_PROTOCOL Implementation\r
 \r
-  Copyright (c) 2015, Hewlett Packard Enterprise Development, L.P.<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
 \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
   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
 \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
     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
   }\r
 \r
   //\r
@@ -130,6 +134,10 @@ OnigurumaMatch (
   //\r
   Start = (OnigUChar*)String;\r
   Region = onig_region_new ();\r
   //\r
   Start = (OnigUChar*)String;\r
   Region = onig_region_new ();\r
+  if (Region == NULL) {\r
+    onig_free (OnigRegex);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
   OnigResult = onig_search (\r
                  OnigRegex,\r
                  Start,\r
   OnigResult = onig_search (\r
                  OnigRegex,\r
                  Start,\r
@@ -139,6 +147,7 @@ OnigurumaMatch (
                  Region,\r
                  ONIG_OPTION_NONE\r
                  );\r
                  Region,\r
                  ONIG_OPTION_NONE\r
                  );\r
+\r
   if (OnigResult >= 0) {\r
     *Result = TRUE;\r
   } else {\r
   if (OnigResult >= 0) {\r
     *Result = TRUE;\r
   } else {\r
@@ -146,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
     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
     }\r
   }\r
 \r
@@ -154,14 +166,30 @@ OnigurumaMatch (
   //\r
   if (*Result && Captures != NULL) {\r
     *CapturesCount = Region->num_regs;\r
   //\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
     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].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
       }\r
     }\r
   }\r
@@ -169,7 +197,7 @@ OnigurumaMatch (
   onig_region_free (Region, 1);\r
   onig_free (OnigRegex);\r
 \r
   onig_region_free (Region, 1);\r
   onig_free (OnigRegex);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r