diff mbox series

net: dsa: mt7530: Add some return-value checks

Message ID 20200916195017.34057-1-alex.dewar90@gmail.com (mailing list archive)
State New
Headers show
Series net: dsa: mt7530: Add some return-value checks | expand

Commit Message

Alex Dewar Sept. 16, 2020, 7:50 p.m. UTC
In mt7531_cpu_port_config(), if the variable port is neither 5 nor 5,
then variable interface will be used uninitialised. Change the function
to return -EINVAL in this case.

As the return value of mt7531_cpu_port_config() is never checked
(even though it returns an int) add a check in the correct place so that
the error can be passed up the call stack. Now that we correctly handle
errors thrown in this function, also check the return value of
mt7531_mac_config() in case an error occurs here.

Addresses-Coverity: 1496993 ("Uninitialized variables")
Fixes: c288575f7810 ("net: dsa: mt7530: Add the support of MT7531 switch")
Signed-off-by: Alex Dewar <alex.dewar90@gmail.com>
---

If it is not expected that these functions will throw errors (i.e.
because the parameters passed will always be correct), we could dispense
with the use of EINVAL errors and just use BUG*() macros instead. Let me
know if you'd rather I fix things up in that way.

Best,
Alex

 drivers/net/dsa/mt7530.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

Comments

Gustavo A. R. Silva Sept. 16, 2020, 8:10 p.m. UTC | #1
On 9/16/20 14:50, Alex Dewar wrote:

[..]

> 
>  drivers/net/dsa/mt7530.c | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
> 

[..]

>  
>  	/* Enable Mediatek header mode on the cpu port */
>  	mt7530_write(priv, MT7530_PVC_P(port),
> @@ -2275,7 +2279,7 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
>  {
>  	struct mt7530_priv *priv = ds->priv;
>  	phy_interface_t interface;
> -	int speed;
> +	int ret, speed;

Don't do this. Instead, declare each variable on its own line. In this case,
speed before ret.

Thanks
--
Gustavo
Alex Dewar Sept. 16, 2020, 9:22 p.m. UTC | #2
[snip] 
> >  
> >  	/* Enable Mediatek header mode on the cpu port */
> >  	mt7530_write(priv, MT7530_PVC_P(port),
> > @@ -2275,7 +2279,7 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
> >  {
> >  	struct mt7530_priv *priv = ds->priv;
> >  	phy_interface_t interface;
> > -	int speed;
> > +	int ret, speed;
> 
> Don't do this. Instead, declare each variable on its own line. In this case,
> speed before ret.

Good point. I'll do this in v2 :-)
> 
> Thanks
> --
> Gustavo
Alex Dewar Sept. 16, 2020, 9:23 p.m. UTC | #3
On Wed, Sep 16, 2020 at 08:50:17PM +0100, Alex Dewar wrote:
> In mt7531_cpu_port_config(), if the variable port is neither 5 nor 5,

This should read "neither 5 nor 6", obviously. I'll fix in v2.
Landen Chao Sept. 17, 2020, 7:32 a.m. UTC | #4
Hi Alex,

Thanks for your review and fixing.
On Thu, 2020-09-17 at 03:50 +0800, Alex Dewar wrote:
[..]
> 
> If it is not expected that these functions will throw errors (i.e.
> because the parameters passed will always be correct), we could dispense
> with the use of EINVAL errors and just use BUG*() macros instead. Let me
> know if you'd rather I fix things up in that way.
The cpu port setting is passed by dts. Use EINVAL to catch unexpected
setting is fine.
> 
> Best,
> Alex
> 
>  drivers/net/dsa/mt7530.c | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
> index 61388945d316..157d0a01faae 100644
> --- a/drivers/net/dsa/mt7530.c
> +++ b/drivers/net/dsa/mt7530.c
> @@ -945,10 +945,14 @@ static int
>  mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
>  {
>  	struct mt7530_priv *priv = ds->priv;
> +	int ret;
>  
>  	/* Setup max capability of CPU port at first */
> -	if (priv->info->cpu_port_config)
> -		priv->info->cpu_port_config(ds, port);
> +	if (priv->info->cpu_port_config) {
> +		ret = priv->info->cpu_port_config(ds, port);
> +		if (ret)
> +			return ret;
> +	}
How about check return value in caller function, mt7530_setup() and
mt7531_setup(), too?
                if (dsa_is_cpu_port(ds, i)) {
                        ret = mt753x_cpu_port_enable(ds, i);
			if (ret)
				return ret;
		} else {
[..]
regards,
landen
diff mbox series

Patch

diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index 61388945d316..157d0a01faae 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -945,10 +945,14 @@  static int
 mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
 {
 	struct mt7530_priv *priv = ds->priv;
+	int ret;
 
 	/* Setup max capability of CPU port at first */
-	if (priv->info->cpu_port_config)
-		priv->info->cpu_port_config(ds, port);
+	if (priv->info->cpu_port_config) {
+		ret = priv->info->cpu_port_config(ds, port);
+		if (ret)
+			return ret;
+	}
 
 	/* Enable Mediatek header mode on the cpu port */
 	mt7530_write(priv, MT7530_PVC_P(port),
@@ -2275,7 +2279,7 @@  mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 {
 	struct mt7530_priv *priv = ds->priv;
 	phy_interface_t interface;
-	int speed;
+	int ret, speed;
 
 	switch (port) {
 	case 5:
@@ -2293,6 +2297,8 @@  mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 
 		priv->p6_interface = interface;
 		break;
+	default:
+		return -EINVAL;
 	}
 
 	if (interface == PHY_INTERFACE_MODE_2500BASEX)
@@ -2300,7 +2306,9 @@  mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 	else
 		speed = SPEED_1000;
 
-	mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
+	ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
+	if (ret)
+		return ret;
 	mt7530_write(priv, MT7530_PMCR_P(port),
 		     PMCR_CPU_PORT_SETTING(priv->id));
 	mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,