Skip to contentSkip to conversation

iio: addac: ad74115: Fix use of uninitialized variable rate

PATCH In review Purva Yeshi 3 participants 7 messages Apr 9, 2025 – Apr 17, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: addac: ad74115: Fix use of uninitialized variable rate

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.

---
 drivers/iio/addac/ad74115.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74115.c +1 -1
index a7e480f2472d..26770c68e5fa 100644
@@ -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)
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 22 lines · Purva Yeshi

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.

---
 drivers/iio/addac/ad74115.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74115.c +0 -0
index a7e480f2472d..26770c68e5fa 100644

drivers/iio/addac/ad74115.c static int _ad74115_get_adc_code(struct ad74115_state *st, · line 820

Quoted 8 lines
drivers/iio/addac/ad74115.c +1 -1
@@ -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.

#2 lore raw
Quoted 29 lines · David Lechner
Quoted 28 lines · Purva Yeshi

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.

---
  drivers/iio/addac/ad74115.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74115.c +1 -1
index a7e480f2472d..26770c68e5fa 100644
@@ -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;
 
Quoted 8 lines

drivers/iio/addac/ad74115.c static int _ad74115_get_adc_code(struct ad74115_state *st, · line 820

Quoted 2 lines
drivers/iio/addac/ad74115.c +0 -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.

Hi,

Thank you for the review and explanation.

Understood — since there's a risk of misuse later (like divide-by-zero), it's best to leave it as is.

Best regards,
Purva

Quoted 2 lines
#3 lore raw
In reply to David Lechner
Quoted 35 lines · David Lechner
Quoted 27 lines · Purva Yeshi

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.

---
 drivers/iio/addac/ad74115.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74115.c +1 -1
index a7e480f2472d..26770c68e5fa 100644
@@ -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;
Quoted 3 lines


  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.

Quoted 3 lines

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á

#4 lore raw
Quoted 43 lines · Nuno Sá
Quoted 29 lines · David Lechner
Quoted 28 lines · Purva Yeshi

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.

---
  drivers/iio/addac/ad74115.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74115.c +1 -1
index a7e480f2472d..26770c68e5fa 100644
@@ -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;
 
Quoted 9 lines

drivers/iio/addac/ad74115.c static int _ad74115_get_adc_code(struct ad74115_state *st, · line 820

Quoted 2 lines
drivers/iio/addac/ad74115.c +0 -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()):

Quoted 7 lines

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

#5 lore raw
Quoted 59 lines · Purva Yeshi
Quoted 45 lines · Nuno Sá
Quoted 37 lines · David Lechner
Quoted 29 lines · Purva Yeshi

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.

---
  drivers/iio/addac/ad74115.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74115.c +1 -1
index a7e480f2472d..26770c68e5fa 100644
@@ -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;
Quoted 3 lines


   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.

Quoted 3 lines

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()):

Quoted 8 lines

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.

Quoted 8 lines

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;

Oh yes, bad copy-paste...

Quoted 9 lines

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.

Well, as said, this is a false positive...

- Nuno Sá

#6 lore raw
Quoted 81 lines · Nuno Sá
Quoted 59 lines · Purva Yeshi
Quoted 45 lines · Nuno Sá
Quoted 31 lines · David Lechner
Quoted 30 lines · Purva Yeshi

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.

---
   drivers/iio/addac/ad74115.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74115.c +1 -1
index a7e480f2472d..26770c68e5fa 100644
@@ -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;
 
Quoted 9 lines
Quoted 2 lines
drivers/iio/addac/ad74115.c +0 -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()):

Quoted 8 lines

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.

Quoted 8 lines

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;

Oh yes, bad copy-paste...

Quoted 9 lines

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.

Quoted 4 lines

Well, as said, this is a false positive...

- Nuno Sá

Hi Nuno,

Thank you for the review. I'll drop the patch.

Best regards,
Purva