public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Sahil Kaushal" <sahil.kaushal@arm.com>
To: devel@edk2.groups.io
Cc: "Ard Biesheuvel" <ardb+tianocore@kernel.org>,
	"Leif Lindholm  " <quic_llindhol@quicinc.com>,
	"Sami Mujawar" <sami.mujawar@arm.com>, "sahil  " <sahil@arm.com>
Subject: [edk2-devel] [edk2-platforms][PATCH V3 11/17] Platform/ARM/NorFlashDxe: Fix memory leak in NorFlashCreateInstance()
Date: Thu, 23 May 2024 16:25:05 +0530	[thread overview]
Message-ID: <20240523105511.13189-12-Sahil.Kaushal@arm.com> (raw)
In-Reply-To: <20240523105511.13189-1-Sahil.Kaushal@arm.com>

From: sahil <sahil@arm.com>

This patch adds error_handler1 and error_handler2 labels in
NorFlashCreateInstance() function to handle the cleanup.

error_handler1: Frees just the Instance structure as the
ShadowBuffer is not allocated yet.

error_handler2: Frees both Instance and Instance->ShadowBuffer.

Signed-off-by: sahil <sahil@arm.com>
---
 Platform/ARM/Drivers/NorFlashDxe/NorFlashDxe.c          | 18 +++++++++++++-----
 Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c | 19 ++++++++++++++-----
 2 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/Platform/ARM/Drivers/NorFlashDxe/NorFlashDxe.c b/Platform/ARM/Drivers/NorFlashDxe/NorFlashDxe.c
index e01b05d91978..fd47bd9e4c63 100644
--- a/Platform/ARM/Drivers/NorFlashDxe/NorFlashDxe.c
+++ b/Platform/ARM/Drivers/NorFlashDxe/NorFlashDxe.c
@@ -135,7 +135,8 @@ NorFlashCreateInstance (
 
   Instance->ShadowBuffer = AllocateRuntimePool (BlockSize);
   if (Instance->ShadowBuffer == NULL) {
-    return EFI_OUT_OF_RESOURCES;
+    Status = EFI_OUT_OF_RESOURCES;
+    goto error_handler1;
   }
 
   if (SupportFvb) {
@@ -152,8 +153,7 @@ NorFlashCreateInstance (
                     NULL
                     );
     if (EFI_ERROR (Status)) {
-      FreePool (Instance);
-      return Status;
+      goto error_handler2;
     }
   } else {
     Status = gBS->InstallMultipleProtocolInterfaces (
@@ -167,12 +167,20 @@ NorFlashCreateInstance (
                     NULL
                     );
     if (EFI_ERROR (Status)) {
-      FreePool (Instance);
-      return Status;
+      goto error_handler2;
     }
   }
 
   *NorFlashInstance = Instance;
+  return EFI_SUCCESS;
+
+error_handler1:
+  FreePool (Instance);
+  return Status;
+
+error_handler2:
+  FreePool (Instance->ShadowBuffer);
+  FreePool (Instance);
   return Status;
 }
 
diff --git a/Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c b/Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c
index 16fe3762e125..17dfe26627dd 100644
--- a/Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c
+++ b/Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c
@@ -129,7 +129,8 @@ NorFlashCreateInstance (
 
   Instance->ShadowBuffer = AllocateRuntimePool (BlockSize);
   if (Instance->ShadowBuffer == NULL) {
-    return EFI_OUT_OF_RESOURCES;
+    Status = EFI_OUT_OF_RESOURCES;
+    goto error_handler1;
   }
 
   if (SupportFvb) {
@@ -142,16 +143,24 @@ NorFlashCreateInstance (
                       &Instance->FvbProtocol
                       );
     if (EFI_ERROR (Status)) {
-      FreePool (Instance);
-      return Status;
+      goto error_handler2;
     }
   } else {
     DEBUG ((DEBUG_ERROR, "standalone MM NOR Flash driver only support FVB.\n"));
-    FreePool (Instance);
-    return EFI_UNSUPPORTED;
+    Status = EFI_UNSUPPORTED;
+    goto error_handler2;
   }
 
   *NorFlashInstance = Instance;
+  return EFI_SUCCESS;
+
+error_handler1:
+  FreePool (Instance);
+  return Status;
+
+error_handler2:
+  FreePool (Instance->ShadowBuffer);
+  FreePool (Instance);
   return Status;
 }
 
-- 
2.25.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119153): https://edk2.groups.io/g/devel/message/119153
Mute This Topic: https://groups.io/mt/106260149/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  parent reply	other threads:[~2024-05-23 10:56 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-23 10:54 [edk2-devel] [edk2-platforms][PATCH V3 00/17] Split NorFlashDxe driver and add CadenceQspiNorFlashDeviceLib library Sahil Kaushal
2024-05-23 10:54 ` [edk2-devel] [edk2-platforms][PATCH V3 01/17] Platform/ARM/NorFlashDxe: Move DiskIo related functions out of NorFlash.c Sahil Kaushal
2024-05-23 15:39   ` Sami Mujawar
2024-05-23 10:54 ` [edk2-devel] [edk2-platforms][PATCH V3 02/17] Platform/ARM/NorFlashDxe: Move NorFlashVirtualNotifyEvent Sahil Kaushal
2024-05-23 10:54 ` [edk2-devel] [edk2-platforms][PATCH V3 03/17] Platform/ARM/NorFlashDxe: Add NorFlashCommon.h header file Sahil Kaushal
2024-05-23 10:54 ` [edk2-devel] [edk2-platforms][PATCH V3 04/17] Platform/ARM/NorFlashDxe: Move flash specific functions to NorFlash.c Sahil Kaushal
2024-05-23 10:54 ` [edk2-devel] [edk2-platforms][PATCH V3 05/17] Platform/ARM/NorFlashDxe: Remove unimplemented functions from NorFlash.h Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 06/17] Platform/ARM/NorFlashDxe: Make local functions STATIC Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 07/17] Platform/ARM: Create NorFlashDeviceLib library interface for flash specific functions Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 08/17] Platform/ARM: Add P30NorFlashDeviceLib Library Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 09/17] Platform/ARM/NorFlashDxe: Switch from NorFlash.c to NorFlashDeviceLib Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 10/17] Platform/ARM: Add HostControllerBaseAddress variable Sahil Kaushal
2024-05-23 10:55 ` Sahil Kaushal [this message]
2024-05-23 15:24   ` [edk2-devel] [edk2-platforms][PATCH V3 11/17] Platform/ARM/NorFlashDxe: Fix memory leak in NorFlashCreateInstance() Sami Mujawar
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 12/17] Platform/ARM: Add optional provision to fetch and print NOR Flash info Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 13/17] Silicon/ARM/NeoverseN1Soc: Enable SCP QSPI flash region Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 14/17] Silicon/ARM/NeoverseN1Soc: NOR flash library for N1Sdp Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 15/17] Platform/ARM: Add CadenceQspiNorFlashDeviceLib for NorFlashDxe Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 16/17] Platform/ARM/N1Sdp: Persistent storage for N1Sdp Sahil Kaushal
2024-05-23 10:55 ` [edk2-devel] [edk2-platforms][PATCH V3 17/17] Platform/ARM/N1Sdp: Enable FaultTolerantWrite Dxe driver " Sahil Kaushal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240523105511.13189-12-Sahil.Kaushal@arm.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox