kernel-janitors.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] pstore/ram_core: Improve exception handling in persistent_ram_new()
@ 2024-01-18 14:06 Markus Elfring
  2024-02-01 18:13 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Markus Elfring @ 2024-01-18 14:06 UTC (permalink / raw)
  To: linux-hardening, kernel-janitors, Guilherme G. Piccoli, Kees Cook,
	Tony Luck
  Cc: LKML, Kunwu Chan

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Thu, 18 Jan 2024 14:57:21 +0100

* Omit an initialisation (for the variable “ret”)
  which became unnecessary with this refactoring
  because a memory allocation failure will be directly indicated
  by a corresponding return statement in an if branch.

* Move a call of the function “kstrdup” before two other statements.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 fs/pstore/ram_core.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/fs/pstore/ram_core.c b/fs/pstore/ram_core.c
index f1848cdd6d34..5047a8502e17 100644
--- a/fs/pstore/ram_core.c
+++ b/fs/pstore/ram_core.c
@@ -586,21 +586,23 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
 			unsigned int memtype, u32 flags, char *label)
 {
 	struct persistent_ram_zone *prz;
-	int ret = -ENOMEM;
+	int ret;

 	prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
 	if (!prz) {
 		pr_err("failed to allocate persistent ram zone\n");
-		goto err;
+		return ERR_PTR(-ENOMEM);
+	}
+
+	prz->label = kstrdup(label, GFP_KERNEL);
+	if (!prz->label) {
+		kfree(prz);
+		return ERR_PTR(-ENOMEM);
 	}

 	/* Initialize general buffer state. */
 	raw_spin_lock_init(&prz->buffer_lock);
 	prz->flags = flags;
-	prz->label = kstrdup(label, GFP_KERNEL);
-	if (!prz->label)
-		goto err;
-
 	ret = persistent_ram_buffer_map(start, size, prz, memtype);
 	if (ret)
 		goto err;
--
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] pstore/ram_core: Improve exception handling in persistent_ram_new()
  2024-01-18 14:06 [PATCH] pstore/ram_core: Improve exception handling in persistent_ram_new() Markus Elfring
@ 2024-02-01 18:13 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2024-02-01 18:13 UTC (permalink / raw)
  To: Markus Elfring
  Cc: linux-hardening, kernel-janitors, Guilherme G. Piccoli, Tony Luck,
	LKML, Kunwu Chan

On Thu, Jan 18, 2024 at 03:06:53PM +0100, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Thu, 18 Jan 2024 14:57:21 +0100
> 
> * Omit an initialisation (for the variable “ret”)
>   which became unnecessary with this refactoring
>   because a memory allocation failure will be directly indicated
>   by a corresponding return statement in an if branch.
> 
> * Move a call of the function “kstrdup” before two other statements.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>

Thanks for you patch!

I've decided not to apply it because I prefer having a single exit path
for error handling, and it works as-is already.

-Kees

> ---
>  fs/pstore/ram_core.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/pstore/ram_core.c b/fs/pstore/ram_core.c
> index f1848cdd6d34..5047a8502e17 100644
> --- a/fs/pstore/ram_core.c
> +++ b/fs/pstore/ram_core.c
> @@ -586,21 +586,23 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
>  			unsigned int memtype, u32 flags, char *label)
>  {
>  	struct persistent_ram_zone *prz;
> -	int ret = -ENOMEM;
> +	int ret;
> 
>  	prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
>  	if (!prz) {
>  		pr_err("failed to allocate persistent ram zone\n");
> -		goto err;
> +		return ERR_PTR(-ENOMEM);
> +	}
> +
> +	prz->label = kstrdup(label, GFP_KERNEL);
> +	if (!prz->label) {
> +		kfree(prz);
> +		return ERR_PTR(-ENOMEM);
>  	}
> 
>  	/* Initialize general buffer state. */
>  	raw_spin_lock_init(&prz->buffer_lock);
>  	prz->flags = flags;
> -	prz->label = kstrdup(label, GFP_KERNEL);
> -	if (!prz->label)
> -		goto err;
> -
>  	ret = persistent_ram_buffer_map(start, size, prz, memtype);
>  	if (ret)
>  		goto err;
> --
> 2.43.0
> 

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-01 18:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-18 14:06 [PATCH] pstore/ram_core: Improve exception handling in persistent_ram_new() Markus Elfring
2024-02-01 18:13 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).