Skip to contentSkip to conversation

net: ethernet: mtk_eth_soc: fix error checking in mtk_mac_config()

PATCH Superseded [email protected] 2 participants 3 messages Jan 8, 2022 – Jan 10, 2022

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ethernet: mtk_eth_soc: fix error checking in mtk_mac_config()

From: Tom Rix <[email protected]>

Clang static analysis reports this problem mtk_eth_soc.c:394:7: warning: Branch condition evaluates
to a garbage value

                if (err)
                    ^~~

err is not initialized and only conditionally set. Check err consistently with the rest of mtk_mac_config(), after even possible setting.

Fixes 7e538372694b ("net: ethernet: mediatek: Re-add support SGMII")
---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +7 -5
index b67b4323cff08..a27e548488584 100644
@@ -385,14 +385,16 @@ static void mtk_mac_config(struct phylink_config *config, unsigned int mode,
 		       0 : mac->id;
 
 		/* Setup SGMIISYS with the determined property */
-		if (state->interface != PHY_INTERFACE_MODE_SGMII)
+		if (state->interface != PHY_INTERFACE_MODE_SGMII) {
 			err = mtk_sgmii_setup_mode_force(eth->sgmii, sid,
 							 state);
-		else if (phylink_autoneg_inband(mode))
+			if (err)
+				goto init_err;
+		} else if (phylink_autoneg_inband(mode)) {
 			err = mtk_sgmii_setup_mode_an(eth->sgmii, sid);
-
-		if (err)
-			goto init_err;
+			if (err)
+				goto init_err;
+		}
 
 		regmap_update_bits(eth->ethsys, ETHSYS_SYSCFG0,
 				   SYSCFG0_SGMII_MASK, val);
Signature · 1 line
2.26.3
maintainer
#1 lore raw
Quoted 34 lines · On Sat, 8 Jan 2022 07:50:03 -0800 [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this problem mtk_eth_soc.c:394:7: warning: Branch condition evaluates
to a garbage value

                if (err)
                    ^~~

err is not initialized and only conditionally set. Check err consistently with the rest of mtk_mac_config(), after even possible setting.

Fixes 7e538372694b ("net: ethernet: mediatek: Re-add support SGMII")
---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +4 -2
index b67b4323cff08..a27e548488584 100644
@@ -385,14 +385,16 @@ static void mtk_mac_config(struct phylink_config *config, unsigned int mode,
 		       0 : mac->id;
 
 		/* Setup SGMIISYS with the determined property */
-		if (state->interface != PHY_INTERFACE_MODE_SGMII)
+		if (state->interface != PHY_INTERFACE_MODE_SGMII) {
 			err = mtk_sgmii_setup_mode_force(eth->sgmii, sid,
 							 state);
-		else if (phylink_autoneg_inband(mode))
+			if (err)
+				goto init_err;
+		} else if (phylink_autoneg_inband(mode)) {

drivers/net/ethernet/mediatek/mtk_eth_soc.c static void mtk_mac_config(struct phylink_config *config, unsigned int mode, · line 400

Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +3 -3
 			err = mtk_sgmii_setup_mode_an(eth->sgmii, sid);
-
-		if (err)
-			goto init_err;
+			if (err)
+				goto init_err;
+		}
 
 		regmap_update_bits(eth->ethsys, ETHSYS_SYSCFG0,
 				   SYSCFG0_SGMII_MASK, val);

Why not init err to 0 before the if or add an else err = 0; branch?

author
#2 lore raw
Quoted 43 lines · Jakub Kicinski
Quoted 42 lines · On Sat, 8 Jan 2022 07:50:03 -0800 [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this problem mtk_eth_soc.c:394:7: warning: Branch condition evaluates
to a garbage value

                 if (err)
                     ^~~

err is not initialized and only conditionally set. Check err consistently with the rest of mtk_mac_config(), after even possible setting.

Fixes 7e538372694b ("net: ethernet: mediatek: Re-add support SGMII")
---
  drivers/net/ethernet/mediatek/mtk_eth_soc.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +7 -5
index b67b4323cff08..a27e548488584 100644
@@ -385,14 +385,16 @@ static void mtk_mac_config(struct phylink_config *config, unsigned int mode,
  		       0 : mac->id;
 
  		/* Setup SGMIISYS with the determined property */
-		if (state->interface != PHY_INTERFACE_MODE_SGMII)
+		if (state->interface != PHY_INTERFACE_MODE_SGMII) {
  			err = mtk_sgmii_setup_mode_force(eth->sgmii, sid,
  							 state);
-		else if (phylink_autoneg_inband(mode))
+			if (err)
+				goto init_err;
+		} else if (phylink_autoneg_inband(mode)) {
  			err = mtk_sgmii_setup_mode_an(eth->sgmii, sid);
-
-		if (err)
-			goto init_err;
+			if (err)
+				goto init_err;
+		}
 
Quoted 3 lines

drivers/net/ethernet/mediatek/mtk_eth_soc.c static void mtk_mac_config(struct phylink_config *config, unsigned int mode, · line 400

Quoted 2 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +0 -0
  		regmap_update_bits(eth->ethsys, ETHSYS_SYSCFG0,
  				   SYSCFG0_SGMII_MASK, val);

Why not init err to 0 before the if or add an else err = 0; branch?

This is the way I would have preferred to do it but the function's existing error handling does it this way.

I'll respin the patch.

Tom

Quoted 1 line