diff mbox series

[1/2,next] net: usb: asix: Fix less than zero comparison of a u16

Message ID 20210608152249.160333-1-colin.king@canonical.com
State Superseded
Headers show
Series [1/2,next] net: usb: asix: Fix less than zero comparison of a u16 | expand

Commit Message

Colin King June 8, 2021, 3:22 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

The comparison of the u16 priv->phy_addr < 0 is always false because
phy_addr is unsigned. Fix this by assigning the return from the call
to function asix_read_phy_addr to int ret and using this for the
less than zero error check comparison.

Addresses-Coverity: ("Unsigned compared against 0")
Fixes: e532a096be0e ("net: usb: asix: ax88772: add phylib support")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/net/usb/asix_devices.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Oleksij Rempel June 8, 2021, 6:11 p.m. UTC | #1
On Tue, Jun 08, 2021 at 04:22:49PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The comparison of the u16 priv->phy_addr < 0 is always false because
> phy_addr is unsigned. Fix this by assigning the return from the call
> to function asix_read_phy_addr to int ret and using this for the
> less than zero error check comparison.
> 
> Addresses-Coverity: ("Unsigned compared against 0")
> Fixes: 7e88b11a862a ("net: usb: asix: refactor asix_read_phy_addr() and handle errors on return")

Here is wrong Fixes tag. This assignment was bogus before this patch.

> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/net/usb/ax88172a.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/usb/ax88172a.c b/drivers/net/usb/ax88172a.c
> index 2e2081346740..e24773bb9398 100644
> --- a/drivers/net/usb/ax88172a.c
> +++ b/drivers/net/usb/ax88172a.c
> @@ -205,7 +205,8 @@ static int ax88172a_bind(struct usbnet *dev, struct usb_interface *intf)
>  		goto free;
>  	}
>  
> -	priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);
> +	ret = asix_read_phy_addr(dev, priv->use_embdphy);
> +	priv->phy_addr = ret;

Ah.. it is same bug in different color :)
You probably wonted to do:
	if (ret < 0)
		goto free;

	priv->phy_addr = ret;

>  	if (priv->phy_addr < 0) {
>  		ret = priv->phy_addr;
>  		goto free;
> -- 
> 2.31.1
> 
>
Dan Carpenter June 9, 2021, 9:27 a.m. UTC | #2
On Tue, Jun 08, 2021 at 04:22:48PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>

> 

> The comparison of the u16 priv->phy_addr < 0 is always false because

> phy_addr is unsigned. Fix this by assigning the return from the call

> to function asix_read_phy_addr to int ret and using this for the

> less than zero error check comparison.

> 

> Addresses-Coverity: ("Unsigned compared against 0")

> Fixes: e532a096be0e ("net: usb: asix: ax88772: add phylib support")

> Signed-off-by: Colin Ian King <colin.king@canonical.com>

> ---

>  drivers/net/usb/asix_devices.c | 5 +++--

>  1 file changed, 3 insertions(+), 2 deletions(-)

> 

> diff --git a/drivers/net/usb/asix_devices.c b/drivers/net/usb/asix_devices.c

> index 57dafb3262d9..211c5a87eb15 100644

> --- a/drivers/net/usb/asix_devices.c

> +++ b/drivers/net/usb/asix_devices.c

> @@ -704,9 +704,10 @@ static int ax88772_init_phy(struct usbnet *dev)

>  	struct asix_common_private *priv = dev->driver_priv;

>  	int ret;

>  

> -	priv->phy_addr = asix_read_phy_addr(dev, true);

> -	if (priv->phy_addr < 0)

> +	ret = asix_read_phy_addr(dev, true);

> +	if (ret < 0)

>  		return priv->phy_addr;


		return ret;

regards,
dan carpenter
Colin King June 9, 2021, 9:51 a.m. UTC | #3
On 08/06/2021 19:11, Oleksij Rempel wrote:
> On Tue, Jun 08, 2021 at 04:22:49PM +0100, Colin King wrote:

>> From: Colin Ian King <colin.king@canonical.com>

>>

>> The comparison of the u16 priv->phy_addr < 0 is always false because

>> phy_addr is unsigned. Fix this by assigning the return from the call

>> to function asix_read_phy_addr to int ret and using this for the

>> less than zero error check comparison.

>>

>> Addresses-Coverity: ("Unsigned compared against 0")

>> Fixes: 7e88b11a862a ("net: usb: asix: refactor asix_read_phy_addr() and handle errors on return")

> 

> Here is wrong Fixes tag. This assignment was bogus before this patch.


I'm not sure that's correct, that commit has the following change in it:

diff --git a/drivers/net/usb/ax88172a.c b/drivers/net/usb/ax88172a.c
index b404c9462dce..c8ca5187eece 100644
--- a/drivers/net/usb/ax88172a.c
+++ b/drivers/net/usb/ax88172a.c
@@ -220,6 +220,11 @@ static int ax88172a_bind(struct usbnet *dev, struct
usb_interface *intf)
        }

        priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);
+       if (priv->phy_addr < 0) {
+               ret = priv->phy_addr;
+               goto free;
+       }
+


> 

>> Signed-off-by: Colin Ian King <colin.king@canonical.com>

>> ---

>>  drivers/net/usb/ax88172a.c | 3 ++-

>>  1 file changed, 2 insertions(+), 1 deletion(-)

>>

>> diff --git a/drivers/net/usb/ax88172a.c b/drivers/net/usb/ax88172a.c

>> index 2e2081346740..e24773bb9398 100644

>> --- a/drivers/net/usb/ax88172a.c

>> +++ b/drivers/net/usb/ax88172a.c

>> @@ -205,7 +205,8 @@ static int ax88172a_bind(struct usbnet *dev, struct usb_interface *intf)

>>  		goto free;

>>  	}

>>  

>> -	priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);

>> +	ret = asix_read_phy_addr(dev, priv->use_embdphy);

>> +	priv->phy_addr = ret;

> 

> Ah.. it is same bug in different color :)

> You probably wonted to do:

> 	if (ret < 0)

> 		goto free;

> 

> 	priv->phy_addr = ret;


Doh, brain failure of mine. I'll send a V2 later today.

> 

>>  	if (priv->phy_addr < 0) {

>>  		ret = priv->phy_addr;

>>  		goto free;

>> -- 

>> 2.31.1

>>

>>

>
Oleksij Rempel June 9, 2021, 11:52 a.m. UTC | #4
On Wed, Jun 09, 2021 at 10:51:09AM +0100, Colin Ian King wrote:
> On 08/06/2021 19:11, Oleksij Rempel wrote:

> > On Tue, Jun 08, 2021 at 04:22:49PM +0100, Colin King wrote:

> >> From: Colin Ian King <colin.king@canonical.com>

> >>

> >> The comparison of the u16 priv->phy_addr < 0 is always false because

> >> phy_addr is unsigned. Fix this by assigning the return from the call

> >> to function asix_read_phy_addr to int ret and using this for the

> >> less than zero error check comparison.

> >>

> >> Addresses-Coverity: ("Unsigned compared against 0")

> >> Fixes: 7e88b11a862a ("net: usb: asix: refactor asix_read_phy_addr() and handle errors on return")

> > 

> > Here is wrong Fixes tag. This assignment was bogus before this patch.

> 

> I'm not sure that's correct, that commit has the following change in it:

> 

> diff --git a/drivers/net/usb/ax88172a.c b/drivers/net/usb/ax88172a.c

> index b404c9462dce..c8ca5187eece 100644

> --- a/drivers/net/usb/ax88172a.c

> +++ b/drivers/net/usb/ax88172a.c

> @@ -220,6 +220,11 @@ static int ax88172a_bind(struct usbnet *dev, struct

> usb_interface *intf)

>         }

> 

>         priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);

> +       if (priv->phy_addr < 0) {

> +               ret = priv->phy_addr;

> +               goto free;

> +       }

> +

> 


Even before my patch asix_read_phy_addr() was returning different
error values. My patch just add check for the return value.

> > 

> >> Signed-off-by: Colin Ian King <colin.king@canonical.com>

> >> ---

> >>  drivers/net/usb/ax88172a.c | 3 ++-

> >>  1 file changed, 2 insertions(+), 1 deletion(-)

> >>

> >> diff --git a/drivers/net/usb/ax88172a.c b/drivers/net/usb/ax88172a.c

> >> index 2e2081346740..e24773bb9398 100644

> >> --- a/drivers/net/usb/ax88172a.c

> >> +++ b/drivers/net/usb/ax88172a.c

> >> @@ -205,7 +205,8 @@ static int ax88172a_bind(struct usbnet *dev, struct usb_interface *intf)

> >>  		goto free;

> >>  	}

> >>  

> >> -	priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);

> >> +	ret = asix_read_phy_addr(dev, priv->use_embdphy);

> >> +	priv->phy_addr = ret;

> > 

> > Ah.. it is same bug in different color :)

> > You probably wonted to do:

> > 	if (ret < 0)

> > 		goto free;

> > 

> > 	priv->phy_addr = ret;

> 

> Doh, brain failure of mine. I'll send a V2 later today.

> 

> > 

> >>  	if (priv->phy_addr < 0) {

> >>  		ret = priv->phy_addr;

> >>  		goto free;

> >> -- 

> >> 2.31.1

> >>

> >>

> > 

> 

> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
diff mbox series

Patch

diff --git a/drivers/net/usb/asix_devices.c b/drivers/net/usb/asix_devices.c
index 57dafb3262d9..211c5a87eb15 100644
--- a/drivers/net/usb/asix_devices.c
+++ b/drivers/net/usb/asix_devices.c
@@ -704,9 +704,10 @@  static int ax88772_init_phy(struct usbnet *dev)
 	struct asix_common_private *priv = dev->driver_priv;
 	int ret;
 
-	priv->phy_addr = asix_read_phy_addr(dev, true);
-	if (priv->phy_addr < 0)
+	ret = asix_read_phy_addr(dev, true);
+	if (ret < 0)
 		return priv->phy_addr;
+	priv->phy_addr = ret;
 
 	snprintf(priv->phy_name, sizeof(priv->phy_name), PHY_ID_FMT,
 		 priv->mdio->id, priv->phy_addr);