Skip to contentSkip to conversation

iio: adc: imx93: fix a signedness bug in imx93_adc_read_raw()

PATCH Applied Dan Carpenter 3 participants 3 messages Feb 14, 2023 – Apr 10, 2023

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

PATCH 1/1 iio: adc: imx93: fix a signedness bug in imx93_adc_read_raw()

The problem is these lines:

	ret = vref_uv = regulator_get_voltage(adc->vref);
	if (ret < 0)

The "ret" variable is type long and "vref_uv" is u32 so that means the condition can never be true on a 64bit system. A negative error code from regulator_get_voltage() would be cast to a high positive u32 value and then remain a high positive value when cast to a long.

The "ret" variable only ever stores ints so it should be declared as an int. We can delete the "vref_uv" variable and use "ret" directly.

Fixes 7d02296ac8b8 ("iio: adc: add imx93 adc support")
---
 drivers/iio/adc/imx93_adc.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/adc/imx93_adc.c +3 -4
index d8de8284e13d..8c68f0cd48f2 100644
@@ -236,8 +236,7 @@ static int imx93_adc_read_raw(struct iio_dev *indio_dev,
 {
 	struct imx93_adc *adc = iio_priv(indio_dev);
 	struct device *dev = adc->dev;
-	long ret;
-	u32 vref_uv;
+	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
@@ -253,10 +252,10 @@ static int imx93_adc_read_raw(struct iio_dev *indio_dev,
 		return IIO_VAL_INT;
 
 	case IIO_CHAN_INFO_SCALE:
-		ret = vref_uv = regulator_get_voltage(adc->vref);
+		ret = regulator_get_voltage(adc->vref);
 		if (ret < 0)
 			return ret;
-		*val = vref_uv / 1000;
+		*val = ret / 1000;
 		*val2 = 12;
 		return IIO_VAL_FRACTIONAL_LOG2;
 
Signature · 1 line
2.35.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines

-----Original Message-----
From: Dan Carpenter <[email protected]>
Sent: 2023年2月14日 23:48
To: Bough Chen <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Shawn Guo <[email protected]>; Sascha Hauer <[email protected]>; Pengutronix Kernel Team <[email protected]>; Fabio Estevam <[email protected]>; dl-linux-imx <[email protected]>; [email protected]; [email protected]
Subject: [PATCH] iio: adc: imx93: fix a signedness bug in imx93_adc_read_raw()

The problem is these lines:

	ret = vref_uv = regulator_get_voltage(adc->vref);
	if (ret < 0)
Quoted 7 lines

The "ret" variable is type long and "vref_uv" is u32 so that means the condition can never be true on a 64bit system. A negative error code from regulator_get_voltage() would be cast to a high positive
u32 value and then remain a high positive value when cast to a long.

The "ret" variable only ever stores ints so it should be declared as an int. We can delete the "vref_uv" variable and use "ret" directly.

Thanks for the catching!

Best Regards
Haibo Chen

Quoted 37 lines
Fixes 7d02296ac8b8 ("iio: adc: add imx93 adc support")
---
 drivers/iio/adc/imx93_adc.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/adc/imx93_adc.c index renamed from drivers/iio/adc/imx93_adc.c +0 -0

d8de8284e13d..8c68f0cd48f2 100644

drivers/iio/adc/imx93_adc.c +3 -5
@@ -236,8 +236,7 @@ static int imx93_adc_read_raw(struct iio_dev
 *indio_dev,  {
 	struct imx93_adc *adc = iio_priv(indio_dev);
 	struct device *dev = adc->dev;
-	long ret;
-	u32 vref_uv;
+	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
@@ -253,10 +252,10 @@ static int imx93_adc_read_raw(struct iio_dev
 *indio_dev,
 		return IIO_VAL_INT;
 
 	case IIO_CHAN_INFO_SCALE:
-		ret = vref_uv = regulator_get_voltage(adc->vref);
+		ret = regulator_get_voltage(adc->vref);
 		if (ret < 0)
 			return ret;
-		*val = vref_uv / 1000;
+		*val = ret / 1000;
 		*val2 = 12;
 		return IIO_VAL_FRACTIONAL_LOG2;
 
--

2.35.1

maintainer Applied
#2 lore raw
Quoted 22 lines · On Wed, 15 Feb 2023 02:16:40 +0000 Bough Chen <[email protected]> wrote:
Quoted 22 lines

-----Original Message-----
From: Dan Carpenter <[email protected]>
Sent: 2023年2月14日 23:48
To: Bough Chen <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Shawn Guo <[email protected]>; Sascha Hauer <[email protected]>; Pengutronix Kernel Team <[email protected]>; Fabio Estevam <[email protected]>; dl-linux-imx <[email protected]>; [email protected]; [email protected]
Subject: [PATCH] iio: adc: imx93: fix a signedness bug in imx93_adc_read_raw()

The problem is these lines:

	ret = vref_uv = regulator_get_voltage(adc->vref);
	if (ret < 0)

The "ret" variable is type long and "vref_uv" is u32 so that means the condition can never be true on a 64bit system. A negative error code from regulator_get_voltage() would be cast to a high positive
u32 value and then remain a high positive value when cast to a long.

Quoted 6 lines
Quoted 2 lines

The "ret" variable only ever stores ints so it should be declared as an int. We can delete the "vref_uv" variable and use "ret" directly.

Thanks for the catching!

Applied to the fixes-togreg branch of iio.git.

Thanks

Jonathan

Quoted 41 lines

Best Regards
Haibo Chen

Quoted 37 lines
Fixes 7d02296ac8b8 ("iio: adc: add imx93 adc support")
---
 drivers/iio/adc/imx93_adc.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/adc/imx93_adc.c index renamed from drivers/iio/adc/imx93_adc.c +0 -0

d8de8284e13d..8c68f0cd48f2 100644

drivers/iio/adc/imx93_adc.c +3 -5
@@ -236,8 +236,7 @@ static int imx93_adc_read_raw(struct iio_dev
 *indio_dev,  {
 	struct imx93_adc *adc = iio_priv(indio_dev);
 	struct device *dev = adc->dev;
-	long ret;
-	u32 vref_uv;
+	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
@@ -253,10 +252,10 @@ static int imx93_adc_read_raw(struct iio_dev
 *indio_dev,
 		return IIO_VAL_INT;
 
 	case IIO_CHAN_INFO_SCALE:
-		ret = vref_uv = regulator_get_voltage(adc->vref);
+		ret = regulator_get_voltage(adc->vref);
 		if (ret < 0)
 			return ret;
-		*val = vref_uv / 1000;
+		*val = ret / 1000;
 		*val2 = 12;
 		return IIO_VAL_FRACTIONAL_LOG2;
 
--

2.35.1