Skip to contentSkip to conversation

GPT: incomplete initialization in allocate_disk_part

PATCH New Heinrich Schuchardt 4 participants 6 messages Sep 21, 2017 – Sep 27, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 GPT: incomplete initialization in allocate_disk_part

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
---
 cmd/gpt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
cmd/gpt.c +2 -2
index 638aa19826..a9c562123f 100644
@@ -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;
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/1 GPT: incomplete initialization in allocate_disk_part

Quoted 21 lines · Heinrich Schuchardt

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
---
  cmd/gpt.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
cmd/gpt.c +0 -0
index 638aa19826..a9c562123f 100644
@@ -190,10 +190,10 @@ static void del_gpt_info(void)
  static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum)
  {

cmd/gpt.c static void del_gpt_info(void) · line 199

Quoted 10 lines
cmd/gpt.c +2 -2
  	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

#2 lore raw

PATCH 1/1 GPT: incomplete initialization in allocate_disk_part

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).

Instead of malloc and memset we can use calloc.

Identified by cppcheck.

Fixes 09a49930e41 GPT: read partition table from device into a data structure
Cc Stefan Roese <[email protected]>

---
v2
use calloc as suggested by Stefan

---
 cmd/gpt.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
cmd/gpt.c +1 -2
index 638aa19826..d4406e3120 100644
@@ -190,10 +190,9 @@ 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 = calloc(1, sizeof(struct disk_part));
 	if (!newpart)
 		return ERR_PTR(-ENOMEM);
-	memset(newpart, '\0', sizeof(newpart));
 
 	newpart->gpt_part_info.start = info->start;
 	newpart->gpt_part_info.size = info->size;
Signature · 1 line
2.11.0
#3 lore raw

PATCH 1/1 GPT: incomplete initialization in allocate_disk_part

Quoted 27 lines · Heinrich Schuchardt

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).

Instead of malloc and memset we can use calloc.

Identified by cppcheck.

Fixes 09a49930e41 GPT: read partition table from device into a data structure
Cc Stefan Roese <[email protected]>

---
v2
use calloc as suggested by Stefan

---
  cmd/gpt.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
cmd/gpt.c +0 -0
index 638aa19826..d4406e3120 100644
@@ -190,10 +190,9 @@ static void del_gpt_info(void)
  static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum)

cmd/gpt.c static void del_gpt_info(void) · line 198

Quoted 10 lines
cmd/gpt.c +1 -2
  {
  	struct disk_part *newpart;
-	newpart = malloc(sizeof(*newpart));
+	newpart = calloc(1, sizeof(struct disk_part));
  	if (!newpart)
  		return ERR_PTR(-ENOMEM);
-	memset(newpart, '\0', sizeof(newpart));
 
  	newpart->gpt_part_info.start = info->start;
  	newpart->gpt_part_info.size = info->size;

Thanks,
Stefan

#4 lore raw

PATCH 1/1 GPT: incomplete initialization in allocate_disk_part

Quoted 12 lines · Stefan Roese
Quoted 11 lines · Heinrich Schuchardt

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).

Instead of malloc and memset we can use calloc.

Identified by cppcheck.

Quoted 10 lines
Quoted 10 lines
Fixes 09a49930e41 GPT: read partition table from device into a data

structure

---
v2
use calloc as suggested by Stefan

---
  cmd/gpt.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
#5 lore raw

PATCH 1/1 GPT: incomplete initialization in allocate_disk_part

In reply to Heinrich Schuchardt
Quoted 8 lines · Heinrich Schuchardt

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).

Instead of malloc and memset we can use calloc.

Quoted 8 lines

Identified by cppcheck.

Fixes 09a49930e41 GPT: read partition table from device into a data structure
Reported-by Coverity (CID: 167228)
Cc Stefan Roese <[email protected]>

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20170926/bb5baed6/attachment.sig>