From: Purva Yeshi <purvayeshi550@gmail.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Jean Delvare <jdelvare@suse.com>,
linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] hwmon: max31827: Fix uninitialized variable lsb_idx in max31827_init_client
Date: Fri, 11 Apr 2025 12:26:11 +0530 [thread overview]
Message-ID: <26872134-1022-4cd5-bd04-215d54c55791@gmail.com> (raw)
In-Reply-To: <f0ad5113-eab9-4e71-8363-1b7a005189c4@roeck-us.net>
On 11/04/25 01:31, Guenter Roeck wrote:
> On Fri, Apr 11, 2025 at 01:18:33AM +0530, Purva Yeshi wrote:
>> Fix Smatch-detected issue:
>> drivers/hwmon/max31827.c:564 max31827_init_client() error:
>> uninitialized symbol 'lsb_idx'.
>>
>> In the max31827_init_client() function, the variable lsb_idx is assigned
>> a value only when data has exactly one bit set (hweight32(data) == 1).
>> If this condition isn't met, lsb_idx remains uninitialized, leading to
>> undefined behavior when it's subsequently used.
>
> That is not correct.
>
>>
>> Ensure that data is non-zero and has exactly one bit set before
>> calling __ffs(data) to determine lsb_idx. Additionally, verify that
>> lsb_idx does not exceed 4. This approach prevents the use of an
>> uninitialized lsb_idx and resolves the Smatch warning.
>>
>> Signed-off-by: Purva Yeshi <purvayeshi550@gmail.com>
>> ---
>> drivers/hwmon/max31827.c | 9 ++++++---
>> 1 file changed, 6 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/hwmon/max31827.c b/drivers/hwmon/max31827.c
>> index 48e8f8ba4d05..c62eaf186d9d 100644
>> --- a/drivers/hwmon/max31827.c
>> +++ b/drivers/hwmon/max31827.c
>> @@ -558,10 +558,13 @@ static int max31827_init_client(struct max31827_state *st,
>> /*
>> * Convert the desired fault queue into register bits.
>> */
>> - if (data != 0)
>> - lsb_idx = __ffs(data);
>
> lsb_idx is assigned if data != 0, not if hweight32(data) == 1 ...
>
>> + if (data == 0 || hweight32(data) != 1) {
>> + dev_err(dev, "Invalid data in adi,fault-q\n");
>> + return -EINVAL;
>> + }
>>
>> - if (hweight32(data) != 1 || lsb_idx > 4) {
>
> ... and if hweight32(data) != 1, it bails out here before using lsb_idx.
> The problem you describe does not exist.
>
> Guenter
Thank you for reviewing the patch and clarifying the behavior regarding
lsb_idx. I understand the existing control flow.
>
>> + lsb_idx = __ffs(data);
>> + if (lsb_idx > 4) {
>> dev_err(dev, "Invalid data in adi,fault-q\n");
>> return -EINVAL;
>> }
>> --
>> 2.34.1
>>
Best regards,
Purva
prev parent reply other threads:[~2025-04-11 6:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-10 19:48 [PATCH] hwmon: max31827: Fix uninitialized variable lsb_idx in max31827_init_client Purva Yeshi
2025-04-10 20:01 ` Guenter Roeck
2025-04-11 6:56 ` Purva Yeshi [this message]
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=26872134-1022-4cd5-bd04-215d54c55791@gmail.com \
--to=purvayeshi550@gmail.com \
--cc=jdelvare@suse.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
/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