Skip to content

Commit

Permalink
iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_…
Browse files Browse the repository at this point in the history
…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.

Signed-off-by: Rasmus Villemoes <[email protected]>
Fixes: fea251b (iio: addac: add AD74413R driver)
Reviewed-by: Nuno Sá <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Cc: <[email protected]>
Signed-off-by: Jonathan Cameron <[email protected]>
  • Loading branch information
Villemoes authored and jic23 committed Nov 23, 2022
1 parent 8aa2e71 commit 980389d
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion drivers/iio/addac/ad74413r.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 980389d

Please sign in to comment.