public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH mtd-utils] misc-utils: flash_erase: FIX integer overflow in flash_erase.c
@ 2024-12-11 14:04 Anton Moryakov
  2024-12-12  1:24 ` Zhihao Cheng
  0 siblings, 1 reply; 2+ messages in thread
From: Anton Moryakov @ 2024-12-11 14:04 UTC (permalink / raw)
  To: linux-mtd, chengzhihao1; +Cc: Anton Moryakov

Report of the static analyzer:
The value of an arithmetic expression 'eb_cnt * mtd.eb_size' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic

Corrections explained:
Added explicit casting of eb_cnt to long long in the condition 
if (eb_start == 0 && mtd.size == eb_cnt * mtd.eb_size) 
to ensure the multiplication is performed in a 64-bit context, 
preventing potential overflow for large values of eb_cnt and mtd.eb_size.
This ensures correct handling of devices with large block counts or block sizes.

Triggers found by static analyzer Svace.

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>

---
 misc-utils/flash_erase.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/misc-utils/flash_erase.c b/misc-utils/flash_erase.c
index c6f6f66..78a3bd1 100644
--- a/misc-utils/flash_erase.c
+++ b/misc-utils/flash_erase.c
@@ -239,7 +239,7 @@ int main(int argc, char *argv[])
 	if (eb_cnt == 0)
 		eb_cnt = (mtd.size / mtd.eb_size) - eb_start;
 
-	if (eb_start == 0 && mtd.size == eb_cnt * mtd.eb_size)
+	if (eb_start == 0 && mtd.size == (long long)eb_cnt * mtd.eb_size)
 		erase_chip = true;
 
 	/* If MTD device may have bad eraseblocks,
-- 
2.30.2


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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH mtd-utils] misc-utils: flash_erase: FIX integer overflow in flash_erase.c
  2024-12-11 14:04 [PATCH mtd-utils] misc-utils: flash_erase: FIX integer overflow in flash_erase.c Anton Moryakov
@ 2024-12-12  1:24 ` Zhihao Cheng
  0 siblings, 0 replies; 2+ messages in thread
From: Zhihao Cheng @ 2024-12-12  1:24 UTC (permalink / raw)
  To: Anton Moryakov, linux-mtd

在 2024/12/11 22:04, Anton Moryakov 写道:
> Report of the static analyzer:
> The value of an arithmetic expression 'eb_cnt * mtd.eb_size' is a subject to overflow because its operands are not cast to a larger data type before performing arithmetic
> 
> Corrections explained:
> Added explicit casting of eb_cnt to long long in the condition
> if (eb_start == 0 && mtd.size == eb_cnt * mtd.eb_size)
> to ensure the multiplication is performed in a 64-bit context,
> preventing potential overflow for large values of eb_cnt and mtd.eb_size.
> This ensures correct handling of devices with large block counts or block sizes.
> 
> Triggers found by static analyzer Svace.
> 
> Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
> 
> ---
>   misc-utils/flash_erase.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
> 
> diff --git a/misc-utils/flash_erase.c b/misc-utils/flash_erase.c
> index c6f6f66..78a3bd1 100644
> --- a/misc-utils/flash_erase.c
> +++ b/misc-utils/flash_erase.c
> @@ -239,7 +239,7 @@ int main(int argc, char *argv[])
>   	if (eb_cnt == 0)
>   		eb_cnt = (mtd.size / mtd.eb_size) - eb_start;
>   
> -	if (eb_start == 0 && mtd.size == eb_cnt * mtd.eb_size)
> +	if (eb_start == 0 && mtd.size == (long long)eb_cnt * mtd.eb_size)
>   		erase_chip = true;
>   
>   	/* If MTD device may have bad eraseblocks,
> 


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-12-12  3:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-11 14:04 [PATCH mtd-utils] misc-utils: flash_erase: FIX integer overflow in flash_erase.c Anton Moryakov
2024-12-12  1:24 ` Zhihao Cheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox