iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
Subsystems (3)
PATCH 1/1 iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
The constant AD74413R_ADC_RESULT_MAX is defined via GENMASK, so its type is "unsigned long".
Hence in the expression voltage_offset * AD74413R_ADC_RESULT_MAX, voltage_offset is first promoted to unsigned long, and since it may be negative, that results in a garbage value. For example, when range is AD74413R_ADC_RANGE_5V_BI_DIR, voltage_offset is -2500 and voltage_range is 5000, so the RHS of this assignment is, depending on sizeof(long), either 826225UL or 3689348814709142UL, which after truncation to int then results in either 826225 or 1972216214 being the output from in_currentX_offset.
Casting to int avoids that promotion and results in the correct -32767 output.
--- drivers/iio/addac/ad74413r.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74413r.c +1 -1
@@ -691,7 +691,7 @@ static int ad74413_get_input_current_offset(struct ad74413r_state *st,if (ret)return ret; - *val = voltage_offset *AD74413R_ADC_RESULT_MAX / voltage_range; + *val = voltage_offset * (int )AD74413R_ADC_RESULT_MAX / voltage_range;return IIO_VAL_INT ; }
Signature · 1 line
2.37.2
Quoted 24 lines
From: Rasmus Villemoes <[email protected]>
Sent: Friday, November 18, 2022 1:32 PM
To: Tanislav, Cosmin <[email protected]>; Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>
Cc: Rasmus Villemoes <[email protected]>; linux- [email protected]; [email protected]
Subject: [PATCH] iio: addac: ad74413r: fix integer promotion bug inad74413_get_input_current_offset()[External]
The constant
AD74413R_ADC_RESULT_MAXis defined via GENMASK, so its type is "unsigned long".Hence in the expression
voltage_offset*AD74413R_ADC_RESULT_MAX,voltage_offsetis first promoted to unsigned long, and since it may be negative, that results in a garbage value. For example, when range isAD74413R_ADC_RANGE_5V_BI_DIR,voltage_offsetis -2500 andvoltage_rangeis 5000, so the RHS of this assignment is, depending on sizeof(long), either 826225UL or 3689348814709142UL, which after truncation to int then results in either 826225 or 1972216214 being the output fromin_currentX_offset.
Quoted 5 lines
Casting to int avoids that promotion and results in the correct -32767 output.
Signed-off-by Rasmus Villemoes <[email protected]>---
After adding proper Fixes: tag,
Quoted 9 lines · Nuno
Quoted 5 lines
Casting to int avoids that promotion and results in the correct -32767 output.
Signed-off-by Rasmus Villemoes <[email protected]>---
After adding proper Fixes: tag,
Reviewed-by Nuno Sá <[email protected]>
That would be
Thanks,
Rasmus
Quoted 8 lines · On Fri, 18 Nov 2022 14:29:23 +0100 Rasmus Villemoes <[email protected]>…
Quoted 6 lines · Nuno
Quoted 5 lines
Casting to int avoids that promotion and results in the correct -32767 output.
Signed-off-by Rasmus Villemoes <[email protected]>---
Quoted 8 lines
Quoted 3 lines
After adding proper Fixes: tag,
Reviewed-by Nuno Sá <[email protected]>That would be
Fixes fea251b6a5db (iio: addac: add AD74413R driver)
Applied to the togreg branch of iio.git (as very late in cycle) and marked for stable.
Thanks,
Jonathan
Quoted 3 lines
Thanks,
Rasmus