Skip to contentSkip to conversation

hwmon: max31827: Fix uninitialized variable lsb_idx in max31827_init_client

PATCH In review Purva Yeshi 2 participants 3 messages Apr 10, 2025 – Apr 11, 2025 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: max31827: Fix uninitialized variable lsb_idx in max31827_init_client

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.

---
 drivers/hwmon/max31827.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/hwmon/max31827.c +6 -3
index 48e8f8ba4d05..c62eaf186d9d 100644
@@ -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;
 		}
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 8 lines · Purva Yeshi

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.

Quoted 16 lines

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.

---
 drivers/hwmon/max31827.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/hwmon/max31827.c +0 -0
index 48e8f8ba4d05..c62eaf186d9d 100644

drivers/hwmon/max31827.c static int max31827_init_client(struct max31827_state *st, · line 562

Quoted 5 lines
drivers/hwmon/max31827.c +0 -2
@@ -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 ...

drivers/hwmon/max31827.c static int max31827_init_client(struct max31827_state *st, · line 564

Quoted 6 lines
drivers/hwmon/max31827.c +4 -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

Quoted 8 lines
drivers/hwmon/max31827.c +2 -0
+		lsb_idx = __ffs(data);
+		if (lsb_idx > 4) {
 			dev_err(dev, "Invalid data in adi,fault-q\n");
 			return -EINVAL;
 		}
Signature · 1 line
2.34.1
#2 lore raw
Quoted 41 lines · Guenter Roeck
Quoted 8 lines · Purva Yeshi

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.

Quoted 16 lines

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.

---
  drivers/hwmon/max31827.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/hwmon/max31827.c +0 -0
index 48e8f8ba4d05..c62eaf186d9d 100644

drivers/hwmon/max31827.c static int max31827_init_client(struct max31827_state *st, · line 562

Quoted 5 lines
drivers/hwmon/max31827.c +0 -2
@@ -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 ...

Quoted 5 lines
drivers/hwmon/max31827.c +4 -0
+		if (data == 0 || hweight32(data) != 1) {
+			dev_err(dev, "Invalid data in adi,fault-q\n");
+			return -EINVAL;
+		}
 
Quoted 6 lines

drivers/hwmon/max31827.c static int max31827_init_client(struct max31827_state *st, · line 564

Quoted 1 line
drivers/hwmon/max31827.c +0 -1
-		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.​

Quoted 9 lines
Quoted 9 lines
drivers/hwmon/max31827.c +2 -0
+		lsb_idx = __ffs(data);
+		if (lsb_idx > 4) {
  			dev_err(dev, "Invalid data in adi,fault-q\n");
  			return -EINVAL;
  		}
Signature · 1 line
2.34.1

Best regards,
Purva