Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
@ 2022-11-18 12:32 Rasmus Villemoes
  2022-11-18 13:17 ` Sa, Nuno
  0 siblings, 1 reply; 4+ messages in thread
From: Rasmus Villemoes @ 2022-11-18 12:32 UTC (permalink / raw)
  To: Cosmin Tanislav, Lars-Peter Clausen, Michael Hennerich,
	Jonathan Cameron
  Cc: Rasmus Villemoes, linux-iio, linux-kernel

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 <linux@rasmusvillemoes.dk>
---
 drivers/iio/addac/ad74413r.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/addac/ad74413r.c b/drivers/iio/addac/ad74413r.c
index 899bcd83f40b..e0e130ba9d3e 100644
--- a/drivers/iio/addac/ad74413r.c
+++ b/drivers/iio/addac/ad74413r.c
@@ -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;
 }
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* RE: [PATCH] iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
  2022-11-18 12:32 [PATCH] iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset() Rasmus Villemoes
@ 2022-11-18 13:17 ` Sa, Nuno
  2022-11-18 13:29   ` Rasmus Villemoes
  0 siblings, 1 reply; 4+ messages in thread
From: Sa, Nuno @ 2022-11-18 13:17 UTC (permalink / raw)
  To: Rasmus Villemoes, Tanislav, Cosmin, Lars-Peter Clausen,
	Hennerich, Michael, Jonathan Cameron
  Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org

> From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> Sent: Friday, November 18, 2022 1:32 PM
> To: Tanislav, Cosmin <Cosmin.Tanislav@analog.com>; Lars-Peter Clausen
> <lars@metafoo.de>; Hennerich, Michael <Michael.Hennerich@analog.com>;
> Jonathan Cameron <jic23@kernel.org>
> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>; linux-
> iio@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: [PATCH] iio: addac: ad74413r: fix integer promotion bug in
> ad74413_get_input_current_offset()
> 
> [External]
> 
> 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 <linux@rasmusvillemoes.dk>
> ---

After adding proper Fixes: tag,

Reviewed-by: Nuno Sá <nuno.sa@analog.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
  2022-11-18 13:17 ` Sa, Nuno
@ 2022-11-18 13:29   ` Rasmus Villemoes
  2022-11-23 20:45     ` Jonathan Cameron
  0 siblings, 1 reply; 4+ messages in thread
From: Rasmus Villemoes @ 2022-11-18 13:29 UTC (permalink / raw)
  To: Sa, Nuno, Tanislav, Cosmin, Lars-Peter Clausen,
	Hennerich, Michael, Jonathan Cameron
  Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org

On 18/11/2022 14.17, Sa, Nuno wrote:

>> Casting to int avoids that promotion and results in the correct -32767
>> output.
>>
>> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
>> ---
> 
> After adding proper Fixes: tag,
> 
> Reviewed-by: Nuno Sá <nuno.sa@analog.com>

That would be

Fixes: fea251b6a5db (iio: addac: add AD74413R driver)

Thanks,
Rasmus


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
  2022-11-18 13:29   ` Rasmus Villemoes
@ 2022-11-23 20:45     ` Jonathan Cameron
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2022-11-23 20:45 UTC (permalink / raw)
  To: Rasmus Villemoes
  Cc: Sa, Nuno, Tanislav, Cosmin, Lars-Peter Clausen,
	Hennerich, Michael, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Fri, 18 Nov 2022 14:29:23 +0100
Rasmus Villemoes <linux@rasmusvillemoes.dk> wrote:

> On 18/11/2022 14.17, Sa, Nuno wrote:
> 
> >> Casting to int avoids that promotion and results in the correct -32767
> >> output.
> >>
> >> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> >> ---  
> > 
> > After adding proper Fixes: tag,
> > 
> > Reviewed-by: Nuno Sá <nuno.sa@analog.com>  
> 
> 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

> Thanks,
> Rasmus
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-11-23 20:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-18 12:32 [PATCH] iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset() Rasmus Villemoes
2022-11-18 13:17 ` Sa, Nuno
2022-11-18 13:29   ` Rasmus Villemoes
2022-11-23 20:45     ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox