public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Purva Yeshi <purvayeshi550@gmail.com>
To: "Nuno Sá" <noname.nuno@gmail.com>,
	"David Lechner" <dlechner@baylibre.com>,
	cosmin.tanislav@analog.com, lars@metafoo.de,
	Michael.Hennerich@analog.com, jic23@kernel.org
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] iio: addac: ad74115: Fix use of uninitialized variable rate
Date: Fri, 11 Apr 2025 14:39:18 +0530	[thread overview]
Message-ID: <f5f40475-fae5-487f-b5ce-dc6c5dfe3600@gmail.com> (raw)
In-Reply-To: <10a9dd5cdf55b6a9845fb9543cdef5f2251ffa6a.camel@gmail.com>

On 11/04/25 11:19, Nuno Sá wrote:
> On Thu, 2025-04-10 at 09:51 -0500, David Lechner wrote:
>> On 4/9/25 3:29 PM, Purva Yeshi wrote:
>>> Fix Smatch-detected error:
>>> drivers/iio/addac/ad74115.c:823 _ad74115_get_adc_code() error:
>>> uninitialized symbol 'rate'.
>>>
>>> The variable rate was declared but not given any value before being used
>>> in a division. If the code reached that point without setting rate, it
>>> would cause unpredictable behavior.
>>>
>>> Declare and initialize 'rate' to zero inside the 'else' block where it is
>>> used. This ensures 'rate' is always initialized before being passed to
>>> DIV_ROUND_CLOSEST.
>>>
>>> Signed-off-by: Purva Yeshi <purvayeshi550@gmail.com>
>>> ---
>>>   drivers/iio/addac/ad74115.c | 2 +-
>>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/iio/addac/ad74115.c b/drivers/iio/addac/ad74115.c
>>> index a7e480f2472d..26770c68e5fa 100644
>>> --- a/drivers/iio/addac/ad74115.c
>>> +++ b/drivers/iio/addac/ad74115.c
>>> @@ -814,7 +814,7 @@ static int _ad74115_get_adc_code(struct ad74115_state *st,
>>>   			return -ETIMEDOUT;
>>>   	} else {
>>>   		unsigned int regval, wait_time;
>>> -		int rate;
>>> +		int rate = 0;
>>>   
>>>   		ret = ad74115_get_adc_rate(st, channel, &rate);
>>>   		if (ret < 0)
>>
>> I don't see how rate could be used uninitialized since we are
>> returning the error if ad74115_get_adc_rate() fails.
>>
>> Also, initializing to 0 would then cause a divide by 0 error
>> if that value was actually used later in the code.
>>
> 
> Agreed... A better check could actually be (in ad74115_get_adc_rate()):
> 
> 
> if (i >= ARRAY_SIZE(ad74115_get_adc_rate))
>      return -EIO;
> 
> Kind of a paranoid check but just making sure a faulty chip does not lead to an out
> of bounds access.
> 
> - Nuno Sá

Hi Nuno,

Thank you for your suggestion regarding the paranoid check.

However, ad74115_get_adc_rate is a function, not an array, pointer, or 
vector. Therefore, using ARRAY_SIZE on it results in a compilation error.

I believe the intended check was:

if (i >= ARRAY_SIZE(ad74115_adc_conv_rate_tbl))
     return -EIO;


This ensures that the index i does not exceed the bounds of the 
ad74115_adc_conv_rate_tbl array, preventing potential out-of-bounds access.

This check prevents potential out-of-bounds access, it does not address 
the Smatch warning about the uninitialized variable 'rate'. Smatch may 
still flag 'rate' as potentially uninitialized if it cannot determine 
that ad74115_get_adc_rate() always initializes it before use.

Best regards,
Purva

  reply	other threads:[~2025-04-11  9:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-09 20:29 [PATCH] iio: addac: ad74115: Fix use of uninitialized variable rate Purva Yeshi
2025-04-10 14:51 ` David Lechner
2025-04-10 17:37   ` Purva Yeshi
2025-04-11  5:49   ` Nuno Sá
2025-04-11  9:09     ` Purva Yeshi [this message]
2025-04-11 12:17       ` Nuno Sá
2025-04-17  5:43         ` Purva Yeshi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f5f40475-fae5-487f-b5ce-dc6c5dfe3600@gmail.com \
    --to=purvayeshi550@gmail.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=cosmin.tanislav@analog.com \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=noname.nuno@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox