diff mbox series

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

Message ID 20241219135103.712032-1-ant.v.moryakov@gmail.com
State Accepted
Delegated to: David Oberhollenzer
Headers show
Series [mtd-utils] misc-utils: add missing error handling for 'bam' allocation in ftl_check.c | expand

Commit Message

Anton Moryakov Dec. 19, 2024, 1:51 p.m. UTC
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>

---
 misc-utils/ftl_check.c | 9 +++++++++
 1 file changed, 9 insertions(+)

http://lists.infradead.org/mailman/listinfo/linux-mtd/

Comments

Zhihao Cheng Dec. 20, 2024, 1:11 a.m. UTC | #1
在 2024/12/19 21:51, 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.
> 
> Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
> 
> ---
>   misc-utils/ftl_check.c | 9 +++++++++
>   1 file changed, 9 insertions(+)

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
> 
> diff --git a/misc-utils/ftl_check.c b/misc-utils/ftl_check.c
> index 5b2dae5..fe43a24 100644
> --- a/misc-utils/ftl_check.c
> +++ b/misc-utils/ftl_check.c
> @@ -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");
>
diff mbox series

Patch

diff --git a/misc-utils/ftl_check.c b/misc-utils/ftl_check.c
index 5b2dae5..fe43a24 100644
--- a/misc-utils/ftl_check.c
+++ b/misc-utils/ftl_check.c
@@ -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");
-- 
2.30.2


______________________________________________________
Linux MTD discussion mailing list