linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: spi-nor: Fix an error code in spi_nor_read_raw()
@ 2019-08-15  8:32 Dan Carpenter
  2019-08-15 10:35 ` walter harms
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Dan Carpenter @ 2019-08-15  8:32 UTC (permalink / raw)
  To: Marek Vasut, Cyrille Pitchen
  Cc: Vignesh Raghavendra, Tudor Ambarus, Richard Weinberger,
	kernel-janitors, linux-mtd, Miquel Raynal, Brian Norris,
	David Woodhouse

The problem is that if "ret" is negative then when we check if
"ret > len", that condition is going to be true because of type
promotion.  So this patch re-orders the code to check for negatives
first and preserve those error codes.

Fixes: f384b352cbf0 ("mtd: spi-nor: parse Serial Flash Discoverable Parameters (SFDP) tables")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/mtd/spi-nor/spi-nor.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
index 63af87609bac..986b0754495d 100644
--- a/drivers/mtd/spi-nor/spi-nor.c
+++ b/drivers/mtd/spi-nor/spi-nor.c
@@ -2903,10 +2903,10 @@ static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
 
 	while (len) {
 		ret = spi_nor_read_data(nor, addr, len, buf);
-		if (!ret || ret > len)
-			return -EIO;
 		if (ret < 0)
 			return ret;
+		if (!ret || ret > len)
+			return -EIO;
 
 		buf += ret;
 		addr += ret;
-- 
2.20.1


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

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

* Re: [PATCH] mtd: spi-nor: Fix an error code in spi_nor_read_raw()
  2019-08-15  8:32 [PATCH] mtd: spi-nor: Fix an error code in spi_nor_read_raw() Dan Carpenter
@ 2019-08-15 10:35 ` walter harms
  2019-08-15 14:41   ` Dan Carpenter
  2019-08-21  7:35 ` Tudor.Ambarus
  2019-08-21  8:21 ` Tudor.Ambarus
  2 siblings, 1 reply; 5+ messages in thread
From: walter harms @ 2019-08-15 10:35 UTC (permalink / raw)
  To: kernel-janitors
  Cc: Vignesh Raghavendra, Tudor Ambarus, Richard Weinberger,
	Marek Vasut, Cyrille Pitchen, linux-mtd, Miquel Raynal,
	Brian Norris, David Woodhouse, Dan Carpenter



Am 15.08.2019 10:32, schrieb Dan Carpenter:
> The problem is that if "ret" is negative then when we check if
> "ret > len", that condition is going to be true because of type
> promotion.  So this patch re-orders the code to check for negatives
> first and preserve those error codes.
> 
> Fixes: f384b352cbf0 ("mtd: spi-nor: parse Serial Flash Discoverable Parameters (SFDP) tables")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
>  drivers/mtd/spi-nor/spi-nor.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
> index 63af87609bac..986b0754495d 100644
> --- a/drivers/mtd/spi-nor/spi-nor.c
> +++ b/drivers/mtd/spi-nor/spi-nor.c
> @@ -2903,10 +2903,10 @@ static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
>  
>  	while (len) {
>  		ret = spi_nor_read_data(nor, addr, len, buf);
> -		if (!ret || ret > len)
> -			return -EIO;
>  		if (ret < 0)
>  			return ret;
> +		if (!ret || ret > len)
> +			return -EIO;

Bonuspoints to make this more readable:

	if (ret==0 || ret > len)
		return -EIO;

that makes the intention more obvious.

JM2C,
 wh

>  
>  		buf += ret;
>  		addr += ret;

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

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

* Re: [PATCH] mtd: spi-nor: Fix an error code in spi_nor_read_raw()
  2019-08-15 10:35 ` walter harms
@ 2019-08-15 14:41   ` Dan Carpenter
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2019-08-15 14:41 UTC (permalink / raw)
  To: walter harms
  Cc: Vignesh Raghavendra, Tudor Ambarus, Richard Weinberger,
	kernel-janitors, Marek Vasut, Cyrille Pitchen, linux-mtd,
	Miquel Raynal, Brian Norris, David Woodhouse

On Thu, Aug 15, 2019 at 12:35:49PM +0200, walter harms wrote:
> 
> 
> Am 15.08.2019 10:32, schrieb Dan Carpenter:
> > The problem is that if "ret" is negative then when we check if
> > "ret > len", that condition is going to be true because of type
> > promotion.  So this patch re-orders the code to check for negatives
> > first and preserve those error codes.
> > 
> > Fixes: f384b352cbf0 ("mtd: spi-nor: parse Serial Flash Discoverable Parameters (SFDP) tables")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> >  drivers/mtd/spi-nor/spi-nor.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
> > index 63af87609bac..986b0754495d 100644
> > --- a/drivers/mtd/spi-nor/spi-nor.c
> > +++ b/drivers/mtd/spi-nor/spi-nor.c
> > @@ -2903,10 +2903,10 @@ static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
> >  
> >  	while (len) {
> >  		ret = spi_nor_read_data(nor, addr, len, buf);
> > -		if (!ret || ret > len)
> > -			return -EIO;
> >  		if (ret < 0)
> >  			return ret;
> > +		if (!ret || ret > len)
> > +			return -EIO;
> 
> Bonuspoints to make this more readable:
> 
> 	if (ret==0 || ret > len)
> 		return -EIO;
> 
> that makes the intention more obvious.

That's not really related to the bug fix.

I do agree with your style though.  "ret" is a number here, and not a
bool or error vs no error so == 0 is nicer.

regards,
dan carpenter


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

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

* Re: [PATCH] mtd: spi-nor: Fix an error code in spi_nor_read_raw()
  2019-08-15  8:32 [PATCH] mtd: spi-nor: Fix an error code in spi_nor_read_raw() Dan Carpenter
  2019-08-15 10:35 ` walter harms
@ 2019-08-21  7:35 ` Tudor.Ambarus
  2019-08-21  8:21 ` Tudor.Ambarus
  2 siblings, 0 replies; 5+ messages in thread
From: Tudor.Ambarus @ 2019-08-21  7:35 UTC (permalink / raw)
  To: dan.carpenter, marek.vasut, Cyrille.Pitchen
  Cc: vigneshr, richard, kernel-janitors, linux-mtd, miquel.raynal,
	computersforpeace, dwmw2



On 08/15/2019 11:32 AM, Dan Carpenter wrote:
> External E-Mail
> 
> 
> The problem is that if "ret" is negative then when we check if
> "ret > len", that condition is going to be true because of type
> promotion.  So this patch re-orders the code to check for negatives
> first and preserve those error codes.
> 
> Fixes: f384b352cbf0 ("mtd: spi-nor: parse Serial Flash Discoverable Parameters (SFDP) tables")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Reviewed-by: Tudor Ambarus <tudor.ambarus@microchip.com>

> ---
>  drivers/mtd/spi-nor/spi-nor.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
> index 63af87609bac..986b0754495d 100644
> --- a/drivers/mtd/spi-nor/spi-nor.c
> +++ b/drivers/mtd/spi-nor/spi-nor.c
> @@ -2903,10 +2903,10 @@ static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
>  
>  	while (len) {
>  		ret = spi_nor_read_data(nor, addr, len, buf);
> -		if (!ret || ret > len)
> -			return -EIO;
>  		if (ret < 0)
>  			return ret;
> +		if (!ret || ret > len)
> +			return -EIO;
>  
>  		buf += ret;
>  		addr += ret;
> 
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [PATCH] mtd: spi-nor: Fix an error code in spi_nor_read_raw()
  2019-08-15  8:32 [PATCH] mtd: spi-nor: Fix an error code in spi_nor_read_raw() Dan Carpenter
  2019-08-15 10:35 ` walter harms
  2019-08-21  7:35 ` Tudor.Ambarus
@ 2019-08-21  8:21 ` Tudor.Ambarus
  2 siblings, 0 replies; 5+ messages in thread
From: Tudor.Ambarus @ 2019-08-21  8:21 UTC (permalink / raw)
  To: dan.carpenter, marek.vasut, Cyrille.Pitchen
  Cc: vigneshr, richard, kernel-janitors, linux-mtd, miquel.raynal,
	computersforpeace, dwmw2



On 08/15/2019 11:32 AM, Dan Carpenter wrote:
> External E-Mail
> 
> 
> The problem is that if "ret" is negative then when we check if
> "ret > len", that condition is going to be true because of type
> promotion.  So this patch re-orders the code to check for negatives
> first and preserve those error codes.
> 
> Fixes: f384b352cbf0 ("mtd: spi-nor: parse Serial Flash Discoverable Parameters (SFDP) tables")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
>  drivers/mtd/spi-nor/spi-nor.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 

Since the bug was not introduced in the previous release and we are quite late
for mtd/fixes,

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git,
spi-nor/next branch.

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

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

end of thread, other threads:[~2019-08-21  8:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-15  8:32 [PATCH] mtd: spi-nor: Fix an error code in spi_nor_read_raw() Dan Carpenter
2019-08-15 10:35 ` walter harms
2019-08-15 14:41   ` Dan Carpenter
2019-08-21  7:35 ` Tudor.Ambarus
2019-08-21  8:21 ` Tudor.Ambarus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).