From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id A5639AC0EFD for ; Thu, 23 May 2024 10:56:16 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=LCNtEZiV/kaDRHCGo1l9PU/RohLWI4THRHHJJ7MNJ2w=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1716461775; v=1; b=uGlGWp9hYfLJFNnMwH98tQkNfzr0pLY2z0L5qahkaQ+qYcw9ctIVmMQuWiSXGXWt4LVA2fK2 +Y811eDv3LP7FLeQdj5HR6tvVUa+0ttvlYKgIj0a8JhWX2nfdkynldxM6PewRjBOwGU1rxeGzb6 4+9rbQUsQGoCsAcoeC83jffc/sgSuQPNR4GuTfiQ8pJWNXTLnyxaVi+R6SFhZsCk1wD1t6DAkdt WJYjME9zj98qfTlGhzi+7E2ssjWHhr1/xkrIjHgt621os4FIQLo0fR7HimLc231L8V3vg8AxMsV Vbgf5s/R2XKXRz3KKcYlmuzAyMK53IkE7Hi2qb6fcwA5w== X-Received: by 127.0.0.2 with SMTP id Z36ZYY7687511xNIvp05f50Q; Thu, 23 May 2024 03:56:15 -0700 X-Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web10.12643.1716461774584602956 for ; Thu, 23 May 2024 03:56:14 -0700 X-Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 500A7339; Thu, 23 May 2024 03:56:38 -0700 (PDT) X-Received: from usa.arm.com (a077434.arm.com [10.162.46.143]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 8D6DF3F766; Thu, 23 May 2024 03:56:12 -0700 (PDT) From: "Sahil Kaushal" To: devel@edk2.groups.io Cc: Ard Biesheuvel , =?UTF-8?q?Leif=20Lindholm=20=C2=A0?= , Sami Mujawar , =?UTF-8?q?sahil=20=C2=A0?= 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 Message-Id: <20240523105511.13189-12-Sahil.Kaushal@arm.com> In-Reply-To: <20240523105511.13189-1-Sahil.Kaushal@arm.com> References: <20240523105511.13189-1-Sahil.Kaushal@arm.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Thu, 23 May 2024 03:56:14 -0700 Resent-From: sahil.kaushal@arm.com Reply-To: devel@edk2.groups.io,sahil.kaushal@arm.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: IAlYkCDxafr6VwZX4DEC4MpJx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=uGlGWp9h; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=arm.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io From: sahil 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 --- 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 ( =0D Instance->ShadowBuffer =3D AllocateRuntimePool (BlockSize);=0D if (Instance->ShadowBuffer =3D=3D NULL) {=0D - return EFI_OUT_OF_RESOURCES;=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + goto error_handler1;=0D }=0D =0D if (SupportFvb) {=0D @@ -152,8 +153,7 @@ NorFlashCreateInstance ( NULL=0D );=0D if (EFI_ERROR (Status)) {=0D - FreePool (Instance);=0D - return Status;=0D + goto error_handler2;=0D }=0D } else {=0D Status =3D gBS->InstallMultipleProtocolInterfaces (=0D @@ -167,12 +167,20 @@ NorFlashCreateInstance ( NULL=0D );=0D if (EFI_ERROR (Status)) {=0D - FreePool (Instance);=0D - return Status;=0D + goto error_handler2;=0D }=0D }=0D =0D *NorFlashInstance =3D Instance;=0D + return EFI_SUCCESS;=0D +=0D +error_handler1:=0D + FreePool (Instance);=0D + return Status;=0D +=0D +error_handler2:=0D + FreePool (Instance->ShadowBuffer);=0D + FreePool (Instance);=0D return Status;=0D }=0D =0D diff --git a/Platform/ARM/Drivers/NorFlashDxe/NorFlashStandaloneMm.c b/Plat= form/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 ( =0D Instance->ShadowBuffer =3D AllocateRuntimePool (BlockSize);=0D if (Instance->ShadowBuffer =3D=3D NULL) {=0D - return EFI_OUT_OF_RESOURCES;=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + goto error_handler1;=0D }=0D =0D if (SupportFvb) {=0D @@ -142,16 +143,24 @@ NorFlashCreateInstance ( &Instance->FvbProtocol=0D );=0D if (EFI_ERROR (Status)) {=0D - FreePool (Instance);=0D - return Status;=0D + goto error_handler2;=0D }=0D } else {=0D DEBUG ((DEBUG_ERROR, "standalone MM NOR Flash driver only support FVB.= \n"));=0D - FreePool (Instance);=0D - return EFI_UNSUPPORTED;=0D + Status =3D EFI_UNSUPPORTED;=0D + goto error_handler2;=0D }=0D =0D *NorFlashInstance =3D Instance;=0D + return EFI_SUCCESS;=0D +=0D +error_handler1:=0D + FreePool (Instance);=0D + return Status;=0D +=0D +error_handler2:=0D + FreePool (Instance->ShadowBuffer);=0D + FreePool (Instance);=0D return Status;=0D }=0D =0D --=20 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] -=-=-=-=-=-=-=-=-=-=-=-