Fixed potential issues to release resources when error occurs.
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / UndiRuntimeDxe / Init.c
index b79c5fe..413ef32 100644 (file)
@@ -287,6 +287,7 @@ Returns:
   EFI_PCI_IO_PROTOCOL       *PciIoFncs;\r
   UINTN                     Len;\r
   UINT64                    Supports;\r
   EFI_PCI_IO_PROTOCOL       *PciIoFncs;\r
   UINTN                     Len;\r
   UINT64                    Supports;\r
+  BOOLEAN                   PciAttributesSaved;\r
 \r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
 \r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
@@ -321,6 +322,8 @@ Returns:
     return Status;\r
   }\r
 \r
     return Status;\r
   }\r
 \r
+  PciAttributesSaved = FALSE;\r
+\r
   Status = gBS->AllocatePool (\r
                   EfiRuntimeServicesData,\r
                   sizeof (UNDI32_DEV),\r
   Status = gBS->AllocatePool (\r
                   EfiRuntimeServicesData,\r
                   sizeof (UNDI32_DEV),\r
@@ -344,8 +347,9 @@ Returns:
                     );\r
 \r
   if (EFI_ERROR (Status)) {\r
                     );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto UndiErrorDeleteDevice;\r
   }\r
   }\r
+  PciAttributesSaved = TRUE;\r
 \r
   //\r
   // allocate and initialize both (old and new) the !pxe structures here,\r
 \r
   //\r
   // allocate and initialize both (old and new) the !pxe structures here,\r
@@ -550,15 +554,17 @@ UndiErrorDeletePxe:
   }\r
 \r
 UndiErrorDeleteDevice:\r
   }\r
 \r
 UndiErrorDeleteDevice:\r
-  //\r
-  // Restore original PCI attributes\r
-  //\r
-  PciIoFncs->Attributes (\r
-                  PciIoFncs,\r
-                  EfiPciIoAttributeOperationSet,\r
-                  UNDI32Device->NicInfo.OriginalPciAttributes,\r
-                  NULL\r
-                  );\r
+  if (PciAttributesSaved == TRUE) {\r
+    //\r
+    // Restore original PCI attributes\r
+    //\r
+    PciIoFncs->Attributes (\r
+                    PciIoFncs,\r
+                    EfiPciIoAttributeOperationSet,\r
+                    UNDI32Device->NicInfo.OriginalPciAttributes,\r
+                    NULL\r
+                    );\r
+  }\r
 \r
   gBS->FreePool (UNDI32Device);\r
 \r
 \r
   gBS->FreePool (UNDI32Device);\r
 \r