public inbox for linux-hwmon@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hwmon: max31827: Fix uninitialized variable lsb_idx in max31827_init_client
@ 2025-04-10 19:48 Purva Yeshi
  2025-04-10 20:01 ` Guenter Roeck
  0 siblings, 1 reply; 3+ messages in thread
From: Purva Yeshi @ 2025-04-10 19:48 UTC (permalink / raw)
  To: Jean Delvare, Guenter Roeck; +Cc: linux-hwmon, linux-kernel, Purva Yeshi

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=y, Size: 1453 bytes --]

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.

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);
+		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) {
+		lsb_idx = __ffs(data);
+		if (lsb_idx > 4) {
 			dev_err(dev, "Invalid data in adi,fault-q\n");
 			return -EINVAL;
 		}
-- 
2.34.1


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

end of thread, other threads:[~2025-04-11  6:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox