iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
Rasmus Villemoes authored
commit 980389d0

 upstream.

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: default avatarRasmus Villemoes <linux@rasmusvillemoes.dk>
Fixes: fea251b6

 (iio: addac: add AD74413R driver)
Reviewed-by: default avatarNuno Sá <nuno.sa@analog.com>
Link: https://lore.kernel.org/r/20221118...
1665c3aa
Name Last commit Last update
..
accel iio: accel: bma400: Fix memory leak in bma400_get_steps_reg()
adc iio: adc128s052: add proper .data members in adc128_of_match table
addac iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
afe iio: afe: rescale: export symbols used during testing
amplifiers iio: amplifiers: ad8366: Fix alignment for DMA safety
buffer iio: core: Simplify the registration of kfifo buffers
cdc iio: cdc: ad7746: Move driver out of staging.
chemical i2c: Make remove callback return void
common iio: common: scmi_sensors: use HZ macro from units.h
dac Merge tag 'char-misc-6.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc
dummy Merge tag 'bitmap-for-5.19-rc1' of https://github.com/norov/linux
filter iio:filter:admv8818: select REGMAP_SPI for ADMV8818
frequency iio: frequency: admv1014: return -EINVAL directly
gyro i2c: Make remove callback return void
health iio: health: afe4404: Fix oob read in afe4404_[read|write]_raw
humidity i2c: Make remove callback return void
imu iio: adis: add '__adis_enable_irq()' implementation
light iio: light: rpr0521: add missing Kconfig dependencies
magnetometer Merge tag 'char-misc-6.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc
multiplexer iio: multiplexer: Make use of device properties
orientation iio: hid-sensors: bind IIO channels alloc to device object
position iio: hid-sensors: Update header includes
potentiometer iio: potentiometer: mcp4131: Fix alignment for DMA safety
potentiostat i2c: Make remove callback return void
pressure iio: pressure: ms5611: changed hardcoded SPI speed to value limited
proximity
resolver
temperature
test
trigger
Kconfig
Makefile
TODO
iio_core.h
iio_core_trigger.h
industrialio-buffer.c
industrialio-configfs.c
industrialio-core.c
industrialio-event.c
industrialio-sw-device.c
industrialio-sw-trigger.c
industrialio-trigger.c
industrialio-triggered-event.c
inkern.c