Skip to contentSkip to conversation

misc-utils: add missing error handling for 'bam' allocation in ftl_check.c

PATCH In review Anton Moryakov 2 participants 2 messages Dec 19, 2024 – Dec 20, 2024 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc-utils: add missing error handling for 'bam' allocation in ftl_check.c

Corrections explained:
Added robust handling for malloc() failure by checking the returnvalueand providing a clear error message.

Triggers found by static analyzer Svace.

---
 misc-utils/ftl_check.c | 9 +++++++++
 1 file changed, 9 insertions(+)
misc-utils/ftl_check.c +4 -0
index 5b2dae5..fe43a24 100644
@@ -120,8 +120,17 @@ static void check_partition(int fd)
 
 	/* Create basic block allocation table for control blocks */
 	nbam = (mtd.erasesize >> hdr.BlockSize);
 	bam = malloc(nbam * sizeof(u_int));
+	if (!bam) {
+		perror("malloc failed");
+		return;
+	}
 
 	for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) {
 		if (lseek(fd, (i << hdr.EraseUnitSize), SEEK_SET) == -1) {
 			perror("seek failed");
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Anton Moryakov

Corrections explained:
Added robust handling for malloc() failure by checking the returnvalueand providing a clear error message.

Triggers found by static analyzer Svace.

---
  misc-utils/ftl_check.c | 9 +++++++++
  1 file changed, 9 insertions(+)
Quoted 9 lines
misc-utils/ftl_check.c +0 -0
index 5b2dae5..fe43a24 100644
@@ -120,8 +120,17 @@ static void check_partition(int fd)
 
  	/* Create basic block allocation table for control blocks */

misc-utils/ftl_check.c static void check_partition(int fd) · line 131

Quoted 10 lines
misc-utils/ftl_check.c +4 -0
  	nbam = (mtd.erasesize >> hdr.BlockSize);
  	bam = malloc(nbam * sizeof(u_int));
+	if (!bam) {
+		perror("malloc failed");
+		return;
+	}
 
  	for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) {
  		if (lseek(fd, (i << hdr.EraseUnitSize), SEEK_SET) == -1) {
  			perror("seek failed");

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/