linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: spi-nor: stm32-quadspi: Fix uninitialized error return code
@ 2017-07-15  8:00 Geert Uytterhoeven
  2017-07-17  9:56 ` Ludovic BARRE
  0 siblings, 1 reply; 4+ messages in thread
From: Geert Uytterhoeven @ 2017-07-15  8:00 UTC (permalink / raw)
  To: Ludovic Barre, Cyrille Pitchen, Marek Vasut, David Woodhouse,
	Brian Norris, Boris Brezillon, Richard Weinberger,
	Maxime Coquelin, Alexandre Torgue
  Cc: Arnd Bergmann, linux-mtd, linux-arm-kernel, linux-kernel,
	Geert Uytterhoeven

With gcc 4.1.2:

    drivers/mtd/spi-nor/stm32-quadspi.c: In function ‘stm32_qspi_tx_poll’:
    drivers/mtd/spi-nor/stm32-quadspi.c:230: warning: ‘ret’ may be used uninitialized in this function

Indeed, if stm32_qspi_cmd.len is zero, ret will be uninitialized.
This length is passed from outside the driver using the
spi_nor.{read,write}{,_reg}() callbacks.

Several functions in drivers/mtd/spi-nor/spi-nor.c (e.g. write_enable(),
write_disable(), and erase_chip()) call spi_nor.write_reg() with a zero
length.

Fix this by returning an explicit zero on success.

Fixes: 0d43d7ab277a048c ("mtd: spi-nor: add driver for STM32 quad spi flash controller")
Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
---
 drivers/mtd/spi-nor/stm32-quadspi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/spi-nor/stm32-quadspi.c b/drivers/mtd/spi-nor/stm32-quadspi.c
index 86c0931543c538c3..ad6a3e1844cbe5ec 100644
--- a/drivers/mtd/spi-nor/stm32-quadspi.c
+++ b/drivers/mtd/spi-nor/stm32-quadspi.c
@@ -240,12 +240,12 @@ static int stm32_qspi_tx_poll(struct stm32_qspi *qspi,
 						 STM32_QSPI_FIFO_TIMEOUT_US);
 		if (ret) {
 			dev_err(qspi->dev, "fifo timeout (stat:%#x)\n", sr);
-			break;
+			return ret;
 		}
 		tx_fifo(buf++, qspi->io_base + QUADSPI_DR);
 	}
 
-	return ret;
+	return 0;
 }
 
 static int stm32_qspi_tx_mm(struct stm32_qspi *qspi,
-- 
2.7.4

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

* Re: [PATCH] mtd: spi-nor: stm32-quadspi: Fix uninitialized error return code
  2017-07-15  8:00 [PATCH] mtd: spi-nor: stm32-quadspi: Fix uninitialized error return code Geert Uytterhoeven
@ 2017-07-17  9:56 ` Ludovic BARRE
  2017-07-17 10:22   ` Geert Uytterhoeven
  0 siblings, 1 reply; 4+ messages in thread
From: Ludovic BARRE @ 2017-07-17  9:56 UTC (permalink / raw)
  To: Geert Uytterhoeven, Cyrille Pitchen, Marek Vasut,
	David Woodhouse, Brian Norris, Boris Brezillon,
	Richard Weinberger, Maxime Coquelin, Alexandre Torgue
  Cc: Arnd Bergmann, linux-mtd, linux-arm-kernel, linux-kernel

Hi Geert


Thanks for your remarks.

Today only write_reg could be call with len=0 (spi-nor.c: 
write_enable/disable, set_4byte, erase_chip)

But your remark make sense to prevent: gcc warning and framework 
evolution ...


-In stm32-quadspi.c transfer data is enabled if tx_data is true (some 
actions under this bool)

stm32_qspi_write_reg has already this protection with "!!(buf && len > 0);"

but we could extend this protection

-    cmd.tx_data = true;
+    cmd.tx_data = !!(len > 0);

In : stm32_qspi_read, stm32_qspi_write, stm32_qspi_read_reg

-And to avoid gcc warning: I prefer initialize "ret" in the beginning of 
function

-    int ret;
+    int ret = 0;

I tested this changes, and it's ok for me.

Geert could you resend a new version, or do you prefer that I take care 
of it


BR
Ludo

On 07/15/2017 10:00 AM, Geert Uytterhoeven wrote:
> With gcc 4.1.2:
>
>      drivers/mtd/spi-nor/stm32-quadspi.c: In function ‘stm32_qspi_tx_poll’:
>      drivers/mtd/spi-nor/stm32-quadspi.c:230: warning: ‘ret’ may be used uninitialized in this function
>
> Indeed, if stm32_qspi_cmd.len is zero, ret will be uninitialized.
> This length is passed from outside the driver using the
> spi_nor.{read,write}{,_reg}() callbacks.
>
> Several functions in drivers/mtd/spi-nor/spi-nor.c (e.g. write_enable(),
> write_disable(), and erase_chip()) call spi_nor.write_reg() with a zero
> length.
>
> Fix this by returning an explicit zero on success.
>
> Fixes: 0d43d7ab277a048c ("mtd: spi-nor: add driver for STM32 quad spi flash controller")
> Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
> ---
>   drivers/mtd/spi-nor/stm32-quadspi.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/mtd/spi-nor/stm32-quadspi.c b/drivers/mtd/spi-nor/stm32-quadspi.c
> index 86c0931543c538c3..ad6a3e1844cbe5ec 100644
> --- a/drivers/mtd/spi-nor/stm32-quadspi.c
> +++ b/drivers/mtd/spi-nor/stm32-quadspi.c
> @@ -240,12 +240,12 @@ static int stm32_qspi_tx_poll(struct stm32_qspi *qspi,
>   						 STM32_QSPI_FIFO_TIMEOUT_US);
>   		if (ret) {
>   			dev_err(qspi->dev, "fifo timeout (stat:%#x)\n", sr);
> -			break;
> +			return ret;
>   		}
>   		tx_fifo(buf++, qspi->io_base + QUADSPI_DR);
>   	}
>   
> -	return ret;
> +	return 0;
>   }
>   
>   static int stm32_qspi_tx_mm(struct stm32_qspi *qspi,

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

* Re: [PATCH] mtd: spi-nor: stm32-quadspi: Fix uninitialized error return code
  2017-07-17  9:56 ` Ludovic BARRE
@ 2017-07-17 10:22   ` Geert Uytterhoeven
  2017-09-15 10:06     ` Ludovic BARRE
  0 siblings, 1 reply; 4+ messages in thread
From: Geert Uytterhoeven @ 2017-07-17 10:22 UTC (permalink / raw)
  To: Ludovic BARRE
  Cc: Cyrille Pitchen, Marek Vasut, David Woodhouse, Brian Norris,
	Boris Brezillon, Richard Weinberger, Maxime Coquelin,
	Alexandre Torgue, Arnd Bergmann, MTD Maling List,
	linux-arm-kernel, linux-kernel

Hi Ludovic,

On Mon, Jul 17, 2017 at 11:56 AM, Ludovic BARRE <ludovic.barre@st.com> wrote:
> Today only write_reg could be call with len=0 (spi-nor.c:
> write_enable/disable, set_4byte, erase_chip)
>
> But your remark make sense to prevent: gcc warning and framework evolution
> ...
>
>
> -In stm32-quadspi.c transfer data is enabled if tx_data is true (some
> actions under this bool)
>
> stm32_qspi_write_reg has already this protection with "!!(buf && len > 0);"
>
> but we could extend this protection
>
> -    cmd.tx_data = true;
> +    cmd.tx_data = !!(len > 0);
>
> In : stm32_qspi_read, stm32_qspi_write, stm32_qspi_read_reg

Ah, I missed the check for !cmd->tx_data at the top of stm32_qspi_tx()

> -And to avoid gcc warning: I prefer initialize "ret" in the beginning of
> function
>
> -    int ret;
> +    int ret = 0;
>
> I tested this changes, and it's ok for me.
>
> Geert could you resend a new version, or do you prefer that I take care of
> it

If you initialized ret at the beginning, you lose the ability to catch newly
introduced similar bugs in the future.

> On 07/15/2017 10:00 AM, Geert Uytterhoeven wrote:
>>
>> With gcc 4.1.2:
>>
>>      drivers/mtd/spi-nor/stm32-quadspi.c: In function
>> ‘stm32_qspi_tx_poll’:
>>      drivers/mtd/spi-nor/stm32-quadspi.c:230: warning: ‘ret’ may be used
>> uninitialized in this function
>>
>> Indeed, if stm32_qspi_cmd.len is zero, ret will be uninitialized.
>> This length is passed from outside the driver using the
>> spi_nor.{read,write}{,_reg}() callbacks.
>>
>> Several functions in drivers/mtd/spi-nor/spi-nor.c (e.g. write_enable(),
>> write_disable(), and erase_chip()) call spi_nor.write_reg() with a zero
>> length.
>>
>> Fix this by returning an explicit zero on success.
>>
>> Fixes: 0d43d7ab277a048c ("mtd: spi-nor: add driver for STM32 quad spi
>> flash controller")
>> Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
>> ---
>>   drivers/mtd/spi-nor/stm32-quadspi.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/mtd/spi-nor/stm32-quadspi.c
>> b/drivers/mtd/spi-nor/stm32-quadspi.c
>> index 86c0931543c538c3..ad6a3e1844cbe5ec 100644
>> --- a/drivers/mtd/spi-nor/stm32-quadspi.c
>> +++ b/drivers/mtd/spi-nor/stm32-quadspi.c
>> @@ -240,12 +240,12 @@ static int stm32_qspi_tx_poll(struct stm32_qspi
>> *qspi,
>>
>> STM32_QSPI_FIFO_TIMEOUT_US);
>>                 if (ret) {
>>                         dev_err(qspi->dev, "fifo timeout (stat:%#x)\n",
>> sr);
>> -                       break;
>> +                       return ret;
>>                 }
>>                 tx_fifo(buf++, qspi->io_base + QUADSPI_DR);
>>         }
>>   -     return ret;
>> +       return 0;
>>   }
>>     static int stm32_qspi_tx_mm(struct stm32_qspi *qspi,

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

* Re: [PATCH] mtd: spi-nor: stm32-quadspi: Fix uninitialized error return code
  2017-07-17 10:22   ` Geert Uytterhoeven
@ 2017-09-15 10:06     ` Ludovic BARRE
  0 siblings, 0 replies; 4+ messages in thread
From: Ludovic BARRE @ 2017-09-15 10:06 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Cyrille Pitchen, Marek Vasut, David Woodhouse, Brian Norris,
	Boris Brezillon, Richard Weinberger, Maxime Coquelin,
	Alexandre Torgue, Arnd Bergmann, MTD Maling List,
	linux-arm-kernel, linux-kernel



On 07/17/2017 12:22 PM, Geert Uytterhoeven wrote:
> Hi Ludovic,
> 
> On Mon, Jul 17, 2017 at 11:56 AM, Ludovic BARRE <ludovic.barre@st.com> wrote:
>> Today only write_reg could be call with len=0 (spi-nor.c:
>> write_enable/disable, set_4byte, erase_chip)
>>
>> But your remark make sense to prevent: gcc warning and framework evolution
>> ...
>>
>>
>> -In stm32-quadspi.c transfer data is enabled if tx_data is true (some
>> actions under this bool)
>>
>> stm32_qspi_write_reg has already this protection with "!!(buf && len > 0);"
>>
>> but we could extend this protection
>>
>> -    cmd.tx_data = true;
>> +    cmd.tx_data = !!(len > 0);
>>
>> In : stm32_qspi_read, stm32_qspi_write, stm32_qspi_read_reg
> 
> Ah, I missed the check for !cmd->tx_data at the top of stm32_qspi_tx()
> 
>> -And to avoid gcc warning: I prefer initialize "ret" in the beginning of
>> function
>>
>> -    int ret;
>> +    int ret = 0;
>>
>> I tested this changes, and it's ok for me.
>>
>> Geert could you resend a new version, or do you prefer that I take care of
>> it
> 
> If you initialized ret at the beginning, you lose the ability to catch newly
> introduced similar bugs in the future.
like discuss in thread https://patchwork.kernel.org/patch/9952849/ with 
Arnd and Geert

Cyrille, Marek: could you take geert patch and abandon
https://patchwork.kernel.org/patch/9952849/
thanks

Acked-by: Ludovic Barre <ludovic.barre@st.com>

> 
>> On 07/15/2017 10:00 AM, Geert Uytterhoeven wrote:
>>>
>>> With gcc 4.1.2:
>>>
>>>       drivers/mtd/spi-nor/stm32-quadspi.c: In function
>>> ‘stm32_qspi_tx_poll’:
>>>       drivers/mtd/spi-nor/stm32-quadspi.c:230: warning: ‘ret’ may be used
>>> uninitialized in this function
>>>
>>> Indeed, if stm32_qspi_cmd.len is zero, ret will be uninitialized.
>>> This length is passed from outside the driver using the
>>> spi_nor.{read,write}{,_reg}() callbacks.
>>>
>>> Several functions in drivers/mtd/spi-nor/spi-nor.c (e.g. write_enable(),
>>> write_disable(), and erase_chip()) call spi_nor.write_reg() with a zero
>>> length.
>>>
>>> Fix this by returning an explicit zero on success.
>>>
>>> Fixes: 0d43d7ab277a048c ("mtd: spi-nor: add driver for STM32 quad spi
>>> flash controller")
>>> Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
>>> ---
>>>    drivers/mtd/spi-nor/stm32-quadspi.c | 4 ++--
>>>    1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/mtd/spi-nor/stm32-quadspi.c
>>> b/drivers/mtd/spi-nor/stm32-quadspi.c
>>> index 86c0931543c538c3..ad6a3e1844cbe5ec 100644
>>> --- a/drivers/mtd/spi-nor/stm32-quadspi.c
>>> +++ b/drivers/mtd/spi-nor/stm32-quadspi.c
>>> @@ -240,12 +240,12 @@ static int stm32_qspi_tx_poll(struct stm32_qspi
>>> *qspi,
>>>
>>> STM32_QSPI_FIFO_TIMEOUT_US);
>>>                  if (ret) {
>>>                          dev_err(qspi->dev, "fifo timeout (stat:%#x)\n",
>>> sr);
>>> -                       break;
>>> +                       return ret;
>>>                  }
>>>                  tx_fifo(buf++, qspi->io_base + QUADSPI_DR);
>>>          }
>>>    -     return ret;
>>> +       return 0;
>>>    }
>>>      static int stm32_qspi_tx_mm(struct stm32_qspi *qspi,
> 
> Gr{oetje,eeting}s,
> 
>                          Geert
> 
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
> 
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
>                                  -- Linus Torvalds
> 

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

end of thread, other threads:[~2017-09-15 10:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-15  8:00 [PATCH] mtd: spi-nor: stm32-quadspi: Fix uninitialized error return code Geert Uytterhoeven
2017-07-17  9:56 ` Ludovic BARRE
2017-07-17 10:22   ` Geert Uytterhoeven
2017-09-15 10:06     ` Ludovic BARRE

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).