From 117efd252bc485084a609c405c312f5a9229e25a Mon Sep 17 00:00:00 2001
From: Anton Moryakov <ant.v.moryakov@gmail.com>
Date: Thu, 19 Dec 2024 16:51:03 +0300
Subject: 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.

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 misc-utils/ftl_check.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/misc-utils/ftl_check.c b/misc-utils/ftl_check.c
index 5b2dae5..fe6a91a 100644
--- a/misc-utils/ftl_check.c
+++ b/misc-utils/ftl_check.c
@@ -121,6 +121,10 @@ 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) {
-- 
cgit v1.2.3