diff mbox series

[1/2] binfmt: Use struct_size()

Message ID 53150beae5dc04dac513dba391a2e4ae8696a7f3.1685290790.git.christophe.jaillet@wanadoo.fr (mailing list archive)
State Handled Elsewhere, archived
Headers show
Series [1/2] binfmt: Use struct_size() | expand

Commit Message

Christophe JAILLET May 28, 2023, 4:20 p.m. UTC
Use struct_size() instead of hand-writing it. It is less verbose, more
robust and more informative.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
Compile tested on arm
---
 fs/binfmt_elf_fdpic.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Comments

Eric W. Biederman May 28, 2023, 4:32 p.m. UTC | #1
Christophe JAILLET <christophe.jaillet@wanadoo.fr> writes:

> Use struct_size() instead of hand-writing it. It is less verbose, more
> robust and more informative.

Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Obviously correct transform.

>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> Compile tested on arm
> ---
>  fs/binfmt_elf_fdpic.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c
> index d76ad3d4f676..237ce388d06d 100644
> --- a/fs/binfmt_elf_fdpic.c
> +++ b/fs/binfmt_elf_fdpic.c
> @@ -748,7 +748,6 @@ static int elf_fdpic_map_file(struct elf_fdpic_params *params,
>  	struct elf32_phdr *phdr;
>  	unsigned long load_addr, stop;
>  	unsigned nloads, tmp;
> -	size_t size;
>  	int loop, ret;
>  
>  	/* allocate a load map table */
> @@ -760,8 +759,7 @@ static int elf_fdpic_map_file(struct elf_fdpic_params *params,
>  	if (nloads == 0)
>  		return -ELIBBAD;
>  
> -	size = sizeof(*loadmap) + nloads * sizeof(*seg);
> -	loadmap = kzalloc(size, GFP_KERNEL);
> +	loadmap = kzalloc(struct_size(loadmap, segs, nloads), GFP_KERNEL);
>  	if (!loadmap)
>  		return -ENOMEM;
Kees Cook May 30, 2023, 10:52 p.m. UTC | #2
On Sun, 28 May 2023 18:20:24 +0200, Christophe JAILLET wrote:
> Use struct_size() instead of hand-writing it. It is less verbose, more
> robust and more informative.
> 
> 

Applied to for-next/execve, thanks!

[1/2] binfmt: Use struct_size()
      https://git.kernel.org/kees/c/e6302d5a285b
[2/2] binfmt: Slightly simplify elf_fdpic_map_file()
      https://git.kernel.org/kees/c/36650a357eac
diff mbox series

Patch

diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c
index d76ad3d4f676..237ce388d06d 100644
--- a/fs/binfmt_elf_fdpic.c
+++ b/fs/binfmt_elf_fdpic.c
@@ -748,7 +748,6 @@  static int elf_fdpic_map_file(struct elf_fdpic_params *params,
 	struct elf32_phdr *phdr;
 	unsigned long load_addr, stop;
 	unsigned nloads, tmp;
-	size_t size;
 	int loop, ret;
 
 	/* allocate a load map table */
@@ -760,8 +759,7 @@  static int elf_fdpic_map_file(struct elf_fdpic_params *params,
 	if (nloads == 0)
 		return -ELIBBAD;
 
-	size = sizeof(*loadmap) + nloads * sizeof(*seg);
-	loadmap = kzalloc(size, GFP_KERNEL);
+	loadmap = kzalloc(struct_size(loadmap, segs, nloads), GFP_KERNEL);
 	if (!loadmap)
 		return -ENOMEM;