linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: Fix uninitialized variable
@ 2024-10-11  9:37 Yo-Jung (Leo) Lin
  2024-10-11 10:54 ` Andy Shevchenko
  2024-10-11 11:52 ` [PATCH v2] " Yo-Jung (Leo) Lin
  0 siblings, 2 replies; 9+ messages in thread
From: Yo-Jung (Leo) Lin @ 2024-10-11  9:37 UTC (permalink / raw)
  Cc: linux-kernel-mentees, ricardo, skhan, 0xff07, Jonathan Cameron,
	Lars-Peter Clausen, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt, Vasileios Amoiridis, Andy Shevchenko,
	Angel Iglesias, Adam Rizkalla, linux-iio, linux-kernel, llvm

clang found that the "offset" in bmp580_trigger_handler doesn't get
initialized before access. Add proper initialization to this variable.

Signed-off-by: Yo-Jung (Leo) Lin <0xff07@gmail.com>
---
 drivers/iio/pressure/bmp280-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
index f4df222ed0c3..7d4151826b86 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -2210,7 +2210,7 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	int ret, offset;
+	int ret, offset = 0;
 
 	guard(mutex)(&data->lock);
 
-- 
2.43.0


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

* Re: [PATCH] iio: Fix uninitialized variable
  2024-10-11  9:37 [PATCH] iio: Fix uninitialized variable Yo-Jung (Leo) Lin
@ 2024-10-11 10:54 ` Andy Shevchenko
  2024-10-11 11:02   ` Javier Carrasco
  2024-10-11 11:52 ` [PATCH v2] " Yo-Jung (Leo) Lin
  1 sibling, 1 reply; 9+ messages in thread
From: Andy Shevchenko @ 2024-10-11 10:54 UTC (permalink / raw)
  To: Yo-Jung (Leo) Lin
  Cc: linux-kernel-mentees, ricardo, skhan, Jonathan Cameron,
	Lars-Peter Clausen, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt, Vasileios Amoiridis, Angel Iglesias,
	Adam Rizkalla, linux-iio, linux-kernel, llvm

On Fri, Oct 11, 2024 at 05:37:45PM +0800, Yo-Jung (Leo) Lin wrote:
> clang found that the "offset" in bmp580_trigger_handler doesn't get
> initialized before access. Add proper initialization to this variable.

...

>  	struct bmp280_data *data = iio_priv(indio_dev);
> -	int ret, offset;
> +	int ret, offset = 0;

Can it be done closer to the actual user of it?


-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH] iio: Fix uninitialized variable
  2024-10-11 10:54 ` Andy Shevchenko
@ 2024-10-11 11:02   ` Javier Carrasco
  0 siblings, 0 replies; 9+ messages in thread
From: Javier Carrasco @ 2024-10-11 11:02 UTC (permalink / raw)
  To: Andy Shevchenko, Yo-Jung (Leo) Lin
  Cc: linux-kernel-mentees, ricardo, skhan, Jonathan Cameron,
	Lars-Peter Clausen, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt, Vasileios Amoiridis, Angel Iglesias,
	Adam Rizkalla, linux-iio, linux-kernel, llvm

On 11/10/2024 12:54, Andy Shevchenko wrote:
> On Fri, Oct 11, 2024 at 05:37:45PM +0800, Yo-Jung (Leo) Lin wrote:
>> clang found that the "offset" in bmp580_trigger_handler doesn't get
>> initialized before access. Add proper initialization to this variable.
> 
> ...
> 
>>  	struct bmp280_data *data = iio_priv(indio_dev);
>> -	int ret, offset;
>> +	int ret, offset = 0;
> 
> Can it be done closer to the actual user of it?
> 
> 

Actually, offset could be initialized to sizeof(32), and only used for
the temperature calculations.

+	int ret, offset = sizeof(s32);

The first memcpy would use 0 as index, as it did before.


Best regards,
Javier Carrasco

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

* [PATCH v2] iio: Fix uninitialized variable
  2024-10-11  9:37 [PATCH] iio: Fix uninitialized variable Yo-Jung (Leo) Lin
  2024-10-11 10:54 ` Andy Shevchenko
@ 2024-10-11 11:52 ` Yo-Jung (Leo) Lin
  2024-10-11 12:31   ` Javier Carrasco
  1 sibling, 1 reply; 9+ messages in thread
From: Yo-Jung (Leo) Lin @ 2024-10-11 11:52 UTC (permalink / raw)
  Cc: linux-kernel-mentees, ricardo, skhan, 0xff07, Jonathan Cameron,
	Lars-Peter Clausen, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt, Vasileios Amoiridis, Andy Shevchenko,
	Angel Iglesias, Adam Rizkalla, linux-iio, linux-kernel, llvm

clang found that the "offset" in bmp580_trigger_handler doesn't get
initialized before access. Add proper initialization to this variable.

Signed-off-by: Yo-Jung (Leo) Lin <0xff07@gmail.com>
---
Change in v2:
- Make value initialization immediate before its first use.
- Link to v1: https://lore.kernel.org/all/20241011093752.30685-1-0xff07@gmail.com/

---
 drivers/iio/pressure/bmp280-core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
index f4df222ed0c3..682329f81886 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -2222,6 +2222,8 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
 		goto out;
 	}
 
+	offset = 0;
+
 	/* Pressure calculations */
 	memcpy(&data->sensor_data[offset], &data->buf[3], 3);
 
-- 
2.43.0


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

* Re: [PATCH v2] iio: Fix uninitialized variable
  2024-10-11 11:52 ` [PATCH v2] " Yo-Jung (Leo) Lin
@ 2024-10-11 12:31   ` Javier Carrasco
  2024-10-11 15:01     ` Yo-Jung Lin
  2024-10-11 18:32     ` Vasileios Aoiridis
  0 siblings, 2 replies; 9+ messages in thread
From: Javier Carrasco @ 2024-10-11 12:31 UTC (permalink / raw)
  To: Yo-Jung (Leo) Lin
  Cc: linux-kernel-mentees, ricardo, skhan, Jonathan Cameron,
	Lars-Peter Clausen, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt, Vasileios Amoiridis, Andy Shevchenko,
	Angel Iglesias, Adam Rizkalla, linux-iio, linux-kernel, llvm

On 11/10/2024 13:52, Yo-Jung (Leo) Lin wrote:
> clang found that the "offset" in bmp580_trigger_handler doesn't get
> initialized before access. Add proper initialization to this variable.
> 
> Signed-off-by: Yo-Jung (Leo) Lin <0xff07@gmail.com>
> ---
> Change in v2:
> - Make value initialization immediate before its first use.
> - Link to v1: https://lore.kernel.org/all/20241011093752.30685-1-0xff07@gmail.com/
> 
> ---
>  drivers/iio/pressure/bmp280-core.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
> index f4df222ed0c3..682329f81886 100644
> --- a/drivers/iio/pressure/bmp280-core.c
> +++ b/drivers/iio/pressure/bmp280-core.c
> @@ -2222,6 +2222,8 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
>  		goto out;
>  	}
>  
> +	offset = 0;
> +
>  	/* Pressure calculations */
>  	memcpy(&data->sensor_data[offset], &data->buf[3], 3);
>  

That was a quick reply. I would recommend you to wait a little bit while
the first version is under discussion.

I still see the offset thing a bit weird. data->sensor_data uses an
offset to avoid hard-coded numbers, but for data->buf we do exactly
that, in the very same lines.

Setting offset to 0 to access the first element i.e. no offset required,
and then adding the actual offset sizeof(s32), which could even be a
const if the first access was to sensor_data[0], looks to verbose.

These things are of course not critical, and the proposed fix is
definitely ok, but I am missing some consistency here.

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

* Re: [PATCH v2] iio: Fix uninitialized variable
  2024-10-11 12:31   ` Javier Carrasco
@ 2024-10-11 15:01     ` Yo-Jung Lin
  2024-10-11 18:32     ` Vasileios Aoiridis
  1 sibling, 0 replies; 9+ messages in thread
From: Yo-Jung Lin @ 2024-10-11 15:01 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: linux-kernel-mentees, ricardo, skhan, Jonathan Cameron,
	Lars-Peter Clausen, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt, Vasileios Amoiridis, Andy Shevchenko,
	Angel Iglesias, Adam Rizkalla, linux-iio, linux-kernel, llvm

Hi Javier,

On Fri, Oct 11, 2024 at 8:31 PM Javier Carrasco
<javier.carrasco.cruz@gmail.com> wrote:
>
> On 11/10/2024 13:52, Yo-Jung (Leo) Lin wrote:
> > clang found that the "offset" in bmp580_trigger_handler doesn't get
> > initialized before access. Add proper initialization to this variable.
> >
> > Signed-off-by: Yo-Jung (Leo) Lin <0xff07@gmail.com>
> > ---
> > Change in v2:
> > - Make value initialization immediate before its first use.
> > - Link to v1: https://lore.kernel.org/all/20241011093752.30685-1-0xff07@gmail.com/
> >
> > ---
> >  drivers/iio/pressure/bmp280-core.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
> > index f4df222ed0c3..682329f81886 100644
> > --- a/drivers/iio/pressure/bmp280-core.c
> > +++ b/drivers/iio/pressure/bmp280-core.c
> > @@ -2222,6 +2222,8 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
> >               goto out;
> >       }
> >
> > +     offset = 0;
> > +
> >       /* Pressure calculations */
> >       memcpy(&data->sensor_data[offset], &data->buf[3], 3);
> >
>
> That was a quick reply. I would recommend you to wait a little bit while
> the first version is under discussion.

It was my bad not waiting for enough feedback to finalize another
patch. I’ll definitely do better next time.

I feel that initializing it as sizeof(s32) in the beginning and not
using it until the second memcpy() only widens the gap between value
initialization and its first access, which doesn’t address
Shevchenko’s concern.

>
> I still see the offset thing a bit weird. data->sensor_data uses an
> offset to avoid hard-coded numbers, but for data->buf we do exactly
> that, in the very same lines.
>
> Setting offset to 0 to access the first element i.e. no offset required,
> and then adding the actual offset sizeof(s32), which could even be a
> const if the first access was to sensor_data[0], looks to verbose.

While that is also true, it’ll take a more general refactoring to make
it driver-wise consistent. Maybe that refactoring should be on top of
a fix, instead of being part of a fix.

>
> These things are of course not critical, and the proposed fix is
> definitely ok, but I am missing some consistency here.

Best,
Leo

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

* Re: [PATCH v2] iio: Fix uninitialized variable
  2024-10-11 12:31   ` Javier Carrasco
  2024-10-11 15:01     ` Yo-Jung Lin
@ 2024-10-11 18:32     ` Vasileios Aoiridis
  2024-10-11 19:32       ` Javier Carrasco
  1 sibling, 1 reply; 9+ messages in thread
From: Vasileios Aoiridis @ 2024-10-11 18:32 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Yo-Jung (Leo) Lin, linux-kernel-mentees, ricardo, skhan,
	Jonathan Cameron, Lars-Peter Clausen, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt, Andy Shevchenko,
	Angel Iglesias, Adam Rizkalla, linux-iio, linux-kernel, llvm

On Fri, Oct 11, 2024 at 02:31:00PM +0200, Javier Carrasco wrote:
> On 11/10/2024 13:52, Yo-Jung (Leo) Lin wrote:
> > clang found that the "offset" in bmp580_trigger_handler doesn't get
> > initialized before access. Add proper initialization to this variable.
> > 
> > Signed-off-by: Yo-Jung (Leo) Lin <0xff07@gmail.com>
> > ---
> > Change in v2:
> > - Make value initialization immediate before its first use.
> > - Link to v1: https://lore.kernel.org/all/20241011093752.30685-1-0xff07@gmail.com/
> > 
> > ---
> >  drivers/iio/pressure/bmp280-core.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
> > index f4df222ed0c3..682329f81886 100644
> > --- a/drivers/iio/pressure/bmp280-core.c
> > +++ b/drivers/iio/pressure/bmp280-core.c
> > @@ -2222,6 +2222,8 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
> >  		goto out;
> >  	}
> >  
> > +	offset = 0;
> > +
> >  	/* Pressure calculations */
> >  	memcpy(&data->sensor_data[offset], &data->buf[3], 3);
> >  
> 
> That was a quick reply. I would recommend you to wait a little bit while
> the first version is under discussion.
> 
> I still see the offset thing a bit weird. data->sensor_data uses an
> offset to avoid hard-coded numbers, but for data->buf we do exactly
> that, in the very same lines.
> 
> Setting offset to 0 to access the first element i.e. no offset required,
> and then adding the actual offset sizeof(s32), which could even be a
> const if the first access was to sensor_data[0], looks to verbose.
> 
> These things are of course not critical, and the proposed fix is
> definitely ok, but I am missing some consistency here.

Hi everyone!

So if you check also the conversations that we had here [1] and in the
previous versions, indeed the idea behind the offset is to use it as an
self-explanatory index to a char buffer that holds in fact s32 variables.

The data->buf here holds the values that have just been read from the
sensor. If you check on the channel specification of this sensor,
you will see ".realbits = 24" in both values that the sensor returns so
hence the value 3.

I am not sure if it makes sense to use a macro here for each one of the
3's that are going to be used only one time each and in order to be more
"consistent". But I might have a wrong view on this one so feel free to
correct me!

For the initialization of the offset indeed, it was already mentioned
here [2] this morning, but on a different patch!!! I couldn't get this
error though with gcc...

Cheers,
Vasilis

[1]: https://lore.kernel.org/all/20240930202353.38203-3-vassilisamir@gmail.com/
[2]: https://lore.kernel.org/linux-iio/202410111221.YIeXHxOv-lkp@intel.com/



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

* Re: [PATCH v2] iio: Fix uninitialized variable
  2024-10-11 18:32     ` Vasileios Aoiridis
@ 2024-10-11 19:32       ` Javier Carrasco
  2024-10-12 10:49         ` Jonathan Cameron
  0 siblings, 1 reply; 9+ messages in thread
From: Javier Carrasco @ 2024-10-11 19:32 UTC (permalink / raw)
  To: Vasileios Aoiridis
  Cc: Yo-Jung (Leo) Lin, linux-kernel-mentees, ricardo, skhan,
	Jonathan Cameron, Lars-Peter Clausen, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt, Andy Shevchenko,
	Angel Iglesias, Adam Rizkalla, linux-iio, linux-kernel, llvm

...

> 
> Hi everyone!
> 
> So if you check also the conversations that we had here [1] and in the
> previous versions, indeed the idea behind the offset is to use it as an
> self-explanatory index to a char buffer that holds in fact s32 variables.
> 
> The data->buf here holds the values that have just been read from the
> sensor. If you check on the channel specification of this sensor,
> you will see ".realbits = 24" in both values that the sensor returns so
> hence the value 3.
> 

So you are using 3 = 24 bits, but s32 not as 4 bytes? the whole thing
would have turned into sensor_data[0], sensor_data[4], and no variables
implied, correct? But I am discussing too much for something that in the
end is more or less the same, I am fine with this proposal.

> I am not sure if it makes sense to use a macro here for each one of the
> 3's that are going to be used only one time each and in order to be more
> "consistent". But I might have a wrong view on this one so feel free to
> correct me!
> 
> For the initialization of the offset indeed, it was already mentioned
> here [2] this morning, but on a different patch!!! I couldn't get this
> error though with gcc...
> 
> Cheers,
> Vasilis
> 

At least for the things I do in the kernel, clang catches more issues
than gcc. Sometimes even gcc will not complain, and clang will fail to
compile (e.g. a goto before a cleanup attribute).

And if you run smatch before sending the series, you might discover a
couple of extra "surprises".

Best regards,
Javier Carrasco

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

* Re: [PATCH v2] iio: Fix uninitialized variable
  2024-10-11 19:32       ` Javier Carrasco
@ 2024-10-12 10:49         ` Jonathan Cameron
  0 siblings, 0 replies; 9+ messages in thread
From: Jonathan Cameron @ 2024-10-12 10:49 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Vasileios Aoiridis, Yo-Jung (Leo) Lin, linux-kernel-mentees,
	ricardo, skhan, Lars-Peter Clausen, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt, Andy Shevchenko,
	Angel Iglesias, Adam Rizkalla, linux-iio, linux-kernel, llvm

On Fri, 11 Oct 2024 21:32:38 +0200
Javier Carrasco <javier.carrasco.cruz@gmail.com> wrote:

> ...
> 
> > 
> > Hi everyone!
> > 
> > So if you check also the conversations that we had here [1] and in the
> > previous versions, indeed the idea behind the offset is to use it as an
> > self-explanatory index to a char buffer that holds in fact s32 variables.
> > 
> > The data->buf here holds the values that have just been read from the
> > sensor. If you check on the channel specification of this sensor,
> > you will see ".realbits = 24" in both values that the sensor returns so
> > hence the value 3.
> >   
> 
> So you are using 3 = 24 bits, but s32 not as 4 bytes? the whole thing
> would have turned into sensor_data[0], sensor_data[4], and no variables
> implied, correct? But I am discussing too much for something that in the
> end is more or less the same, I am fine with this proposal.
Applied the fix.  Thanks,

Jonathan


> 
> > I am not sure if it makes sense to use a macro here for each one of the
> > 3's that are going to be used only one time each and in order to be more
> > "consistent". But I might have a wrong view on this one so feel free to
> > correct me!
> > 
> > For the initialization of the offset indeed, it was already mentioned
> > here [2] this morning, but on a different patch!!! I couldn't get this
> > error though with gcc...
> > 
> > Cheers,
> > Vasilis
> >   
> 
> At least for the things I do in the kernel, clang catches more issues
> than gcc. Sometimes even gcc will not complain, and clang will fail to
> compile (e.g. a goto before a cleanup attribute).
> 
> And if you run smatch before sending the series, you might discover a
> couple of extra "surprises".
> 
> Best regards,
> Javier Carrasco


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

end of thread, other threads:[~2024-10-12 10:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-11  9:37 [PATCH] iio: Fix uninitialized variable Yo-Jung (Leo) Lin
2024-10-11 10:54 ` Andy Shevchenko
2024-10-11 11:02   ` Javier Carrasco
2024-10-11 11:52 ` [PATCH v2] " Yo-Jung (Leo) Lin
2024-10-11 12:31   ` Javier Carrasco
2024-10-11 15:01     ` Yo-Jung Lin
2024-10-11 18:32     ` Vasileios Aoiridis
2024-10-11 19:32       ` Javier Carrasco
2024-10-12 10:49         ` Jonathan Cameron

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