Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1713252 > unrolled thread

[PATCH 2/3] pstore: Improve a size determination in three functions

Started bySF Markus Elfring <elfring@users.sourceforge.net>
First post2017-08-16 21:30 +0200
Last post2017-08-16 22:10 +0200
Articles 3 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 2/3] pstore: Improve a size determination in three functions SF Markus Elfring <elfring@users.sourceforge.net> - 2017-08-16 21:30 +0200
    Re: [PATCH 2/3] pstore: Improve a size determination in three functions Kees Cook <keescook@chromium.org> - 2017-08-16 21:40 +0200
      Re: pstore: Improve a size determination in three functions SF Markus Elfring <elfring@users.sourceforge.net> - 2017-08-16 22:10 +0200

#1713252 — [PATCH 2/3] pstore: Improve a size determination in three functions

FromSF Markus Elfring <elfring@users.sourceforge.net>
Date2017-08-16 21:30 +0200
Subject[PATCH 2/3] pstore: Improve a size determination in three functions
Message-ID<ufcaS-2DT-7@gated-at.bofh.it>
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 16 Aug 2017 20:50:15 +0200

Replace the specification of data types by pointer dereferences
as the parameter for the operator "sizeof" to make the corresponding size
determination a bit safer according to the Linux coding style convention.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 fs/pstore/ram.c      | 3 +--
 fs/pstore/ram_core.c | 4 ++--
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c
index 42d27e5fac9f..0ef95c384bed 100644
--- a/fs/pstore/ram.c
+++ b/fs/pstore/ram.c
@@ -293,8 +293,7 @@ static ssize_t ramoops_pstore_read(struct pstore_record *record)
 			 */
 			struct persistent_ram_zone *tmp_prz, *prz_next;
 
-			tmp_prz = kzalloc(sizeof(struct persistent_ram_zone),
-					  GFP_KERNEL);
+			tmp_prz = kzalloc(sizeof(*tmp_prz), GFP_KERNEL);
 			if (!tmp_prz)
 				return -ENOMEM;
 			free_prz = true;
diff --git a/fs/pstore/ram_core.c b/fs/pstore/ram_core.c
index fafa8af1289c..5d9f7280d757 100644
--- a/fs/pstore/ram_core.c
+++ b/fs/pstore/ram_core.c
@@ -405,7 +405,7 @@ static void *persistent_ram_vmap(phys_addr_t start, size_t size,
 	else
 		prot = pgprot_writecombine(PAGE_KERNEL);
 
-	pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
+	pages = kmalloc_array(page_count, sizeof(*pages), GFP_KERNEL);
 	if (!pages)
 		return NULL;
 
@@ -520,7 +520,7 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
 	struct persistent_ram_zone *prz;
 	int ret = -ENOMEM;
 
-	prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
+	prz = kzalloc(sizeof(*prz), GFP_KERNEL);
 	if (!prz)
 		goto err;
 
-- 
2.14.0

[toc] | [next] | [standalone]


#1713269

FromKees Cook <keescook@chromium.org>
Date2017-08-16 21:40 +0200
Message-ID<ufckz-2IV-31@gated-at.bofh.it>
In reply to#1713252
On Wed, Aug 16, 2017 at 12:22 PM, SF Markus Elfring
<elfring@users.sourceforge.net> wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Wed, 16 Aug 2017 20:50:15 +0200
>
> Replace the specification of data types by pointer dereferences
> as the parameter for the operator "sizeof" to make the corresponding size
> determination a bit safer according to the Linux coding style convention.

Agreed; this is a robustness change in that changes to the variable
structure will be correctly flowed through to the allocations.

>
> This issue was detected by using the Coccinelle software.

Which script detected this?

-Kees

>
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  fs/pstore/ram.c      | 3 +--
>  fs/pstore/ram_core.c | 4 ++--
>  2 files changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c
> index 42d27e5fac9f..0ef95c384bed 100644
> --- a/fs/pstore/ram.c
> +++ b/fs/pstore/ram.c
> @@ -293,8 +293,7 @@ static ssize_t ramoops_pstore_read(struct pstore_record *record)
>                          */
>                         struct persistent_ram_zone *tmp_prz, *prz_next;
>
> -                       tmp_prz = kzalloc(sizeof(struct persistent_ram_zone),
> -                                         GFP_KERNEL);
> +                       tmp_prz = kzalloc(sizeof(*tmp_prz), GFP_KERNEL);
>                         if (!tmp_prz)
>                                 return -ENOMEM;
>                         free_prz = true;
> diff --git a/fs/pstore/ram_core.c b/fs/pstore/ram_core.c
> index fafa8af1289c..5d9f7280d757 100644
> --- a/fs/pstore/ram_core.c
> +++ b/fs/pstore/ram_core.c
> @@ -405,7 +405,7 @@ static void *persistent_ram_vmap(phys_addr_t start, size_t size,
>         else
>                 prot = pgprot_writecombine(PAGE_KERNEL);
>
> -       pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
> +       pages = kmalloc_array(page_count, sizeof(*pages), GFP_KERNEL);
>         if (!pages)
>                 return NULL;
>
> @@ -520,7 +520,7 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
>         struct persistent_ram_zone *prz;
>         int ret = -ENOMEM;
>
> -       prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
> +       prz = kzalloc(sizeof(*prz), GFP_KERNEL);
>         if (!prz)
>                 goto err;
>
> --
> 2.14.0
>



-- 
Kees Cook
Pixel Security

[toc] | [prev] | [next] | [standalone]


#1713287 — Re: pstore: Improve a size determination in three functions

FromSF Markus Elfring <elfring@users.sourceforge.net>
Date2017-08-16 22:10 +0200
SubjectRe: pstore: Improve a size determination in three functions
Message-ID<ufcNA-39W-5@gated-at.bofh.it>
In reply to#1713269
> Which script detected this?

* checkpatch.pl

* The following approach for the semantic patch language.

@replacement@
identifier action, var, work;
statement s1, s2;
statement list sl;
type T, X;
@@
 T work(...)
 {
 ... when any
 X* var;
 ... when any
 var = action(...,
              sizeof(
-                    X
+                    *var
                    ),
              ...
             )
 ... when any
 }

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web