diff mbox series

[U-Boot,1/1] GPT: incomplete initialization in allocate_disk_part

Message ID 20170921105129.21890-1-xypron.glpk@gmx.de
State Changes Requested
Delegated to: Stefan Roese
Headers show
Series [U-Boot,1/1] GPT: incomplete initialization in allocate_disk_part | expand

Commit Message

Heinrich Schuchardt Sept. 21, 2017, 10:51 a.m. UTC
memset(newpart, '\0', sizeof(newpart));
only initializes the firest 4 or 8 bytes of *newpart and not the whole
structure disk_part.

We should use sizeof(struct disk_part).

Identified by cppcheck.

Fixes: 09a49930e41 GPT: read partition table from device into a data structure
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 cmd/gpt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Stefan Roese Sept. 21, 2017, 11:01 a.m. UTC | #1
On 21.09.2017 12:51, Heinrich Schuchardt wrote:
> memset(newpart, '\0', sizeof(newpart));
> only initializes the firest 4 or 8 bytes of *newpart and not the whole
> structure disk_part.
> 
> We should use sizeof(struct disk_part).
> 
> Identified by cppcheck.
> 
> Fixes: 09a49930e41 GPT: read partition table from device into a data structure
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
>   cmd/gpt.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/cmd/gpt.c b/cmd/gpt.c
> index 638aa19826..a9c562123f 100644
> --- a/cmd/gpt.c
> +++ b/cmd/gpt.c
> @@ -190,10 +190,10 @@ static void del_gpt_info(void)
>   static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum)
>   {
>   	struct disk_part *newpart;
> -	newpart = malloc(sizeof(*newpart));
> +	newpart = malloc(sizeof(struct disk_part));
>   	if (!newpart)
>   		return ERR_PTR(-ENOMEM);
> -	memset(newpart, '\0', sizeof(newpart));
> +	memset(newpart, '\0', sizeof(struct disk_part));
>   
>   	newpart->gpt_part_info.start = info->start;
>   	newpart->gpt_part_info.size = info->size;

Why don't you use calloc() instead and drop the memset completely?

Thanks,
Stefan
diff mbox series

Patch

diff --git a/cmd/gpt.c b/cmd/gpt.c
index 638aa19826..a9c562123f 100644
--- a/cmd/gpt.c
+++ b/cmd/gpt.c
@@ -190,10 +190,10 @@  static void del_gpt_info(void)
 static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum)
 {
 	struct disk_part *newpart;
-	newpart = malloc(sizeof(*newpart));
+	newpart = malloc(sizeof(struct disk_part));
 	if (!newpart)
 		return ERR_PTR(-ENOMEM);
-	memset(newpart, '\0', sizeof(newpart));
+	memset(newpart, '\0', sizeof(struct disk_part));
 
 	newpart->gpt_part_info.start = info->start;
 	newpart->gpt_part_info.size = info->size;