[net-next,2/2] net: txgbe: Fix unsigned comparison to zero in txgbe_calc_eeprom_checksum()

Message ID 20221105080722.20292-3-yuehaibing@huawei.com
State New
Headers
Series net: txgbe: Fix two bugs in txgbe_calc_eeprom_checksum |

Commit Message

Yue Haibing Nov. 5, 2022, 8:07 a.m. UTC
  The error checks on checksum for a negative error return always fails because
it is unsigned and can never be negative.

Fixes: 049fe5365324 ("net: txgbe: Add operations to interact with firmware")
Signed-off-by: YueHaibing <yuehaibing@huawei.com>
---
 drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

Jiawen Wu July 11, 2023, 2:47 a.m. UTC | #1
On Saturday, November 5, 2022 4:07 PM, YueHaibing wrote:
> The error checks on checksum for a negative error return always fails because
> it is unsigned and can never be negative.
> 
> Fixes: 049fe5365324 ("net: txgbe: Add operations to interact with firmware")
> Signed-off-by: YueHaibing <yuehaibing@huawei.com>
> ---
>  drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c b/drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c
> index 9cf5fe33118e..167f7ff73192 100644
> --- a/drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c
> +++ b/drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c
> @@ -200,10 +200,11 @@ static int txgbe_calc_eeprom_checksum(struct txgbe_hw *hw, u16 *checksum)
>  	if (eeprom_ptrs)
>  		kvfree(eeprom_ptrs);
> 
> -	*checksum = TXGBE_EEPROM_SUM - *checksum;
> -	if (*checksum < 0)
> +	if (*checksum > TXGBE_EEPROM_SUM)
>  		return -EINVAL;
> 
> +	*checksum = TXGBE_EEPROM_SUM - *checksum;
> +
>  	return 0;
>  }

It is a pity, I didn't review this patch carefully. *checksum will sometimes
be larger than TXGBE_EEPROM_SUM. It's correct to remove these two lines:

-	if (*checksum < 0)
-		return -EINVAL;

I'll send a patch to fix it.
  

Patch

diff --git a/drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c b/drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c
index 9cf5fe33118e..167f7ff73192 100644
--- a/drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c
+++ b/drivers/net/ethernet/wangxun/txgbe/txgbe_hw.c
@@ -200,10 +200,11 @@  static int txgbe_calc_eeprom_checksum(struct txgbe_hw *hw, u16 *checksum)
 	if (eeprom_ptrs)
 		kvfree(eeprom_ptrs);
 
-	*checksum = TXGBE_EEPROM_SUM - *checksum;
-	if (*checksum < 0)
+	if (*checksum > TXGBE_EEPROM_SUM)
 		return -EINVAL;
 
+	*checksum = TXGBE_EEPROM_SUM - *checksum;
+
 	return 0;
 }