Skip to contentSkip to conversation

iioc: dac: ltc2664: Fix span variable usage in ltc2664_channel_config function

PATCH Superseded Mohammed Anees 2 participants 2 messages Oct 4, 2024 – Oct 5, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iioc: dac: ltc2664: Fix span variable usage in ltc2664_channel_config function

In the current implementation of the ltc2664_channel_config function, a variable named span is declared and initialized to 0, intended to capture the return value of the ltc2664_set_span function. However, the output of ltc2664_set_span is directly assigned to chan->span, leaving span unchanged. As a result, when the function later checks if (span < 0), this condition will never trigger an error since span remains 0, this flaw leads to ineffective error handling. The current patch resolves this issue by checking chan->span rather than span and also effectively removing span variable.

---
 drivers/iio/dac/ltc2664.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/dac/ltc2664.c +5 -5
index 5be5345ac5c8..64c8a51aad81 100644
@@ -516,7 +516,7 @@ static int ltc2664_channel_config(struct ltc2664_state *st)
 	const struct ltc2664_chip_info *chip_info = st->chip_info;
 	struct device *dev = &st->spi->dev;
 	u32 reg, tmp[2], mspan;
-	int ret, span = 0;
+	int ret;
 
 	mspan = LTC2664_MSPAN_SOFTSPAN;
 	ret = device_property_read_u32(dev, "adi,manual-span-operation-config",
@@ -581,8 +581,8 @@ static int ltc2664_channel_config(struct ltc2664_state *st)
 		if (!ret && mspan == LTC2664_MSPAN_SOFTSPAN) {
 			chan->span = ltc2664_set_span(st, tmp[0] / 1000,
 						      tmp[1] / 1000, reg);
-			if (span < 0)
-				return dev_err_probe(dev, span,
+			if (chan->span < 0)
+				return dev_err_probe(dev, chan->span,
 						     "Failed to set span\n");
 		}
 
@@ -590,8 +590,8 @@ static int ltc2664_channel_config(struct ltc2664_state *st)
 						     tmp, ARRAY_SIZE(tmp));
 		if (!ret) {
 			chan->span = ltc2664_set_span(st, 0, tmp[1] / 1000, reg);
-			if (span < 0)
-				return dev_err_probe(dev, span,
+			if (chan->span < 0)
+				return dev_err_probe(dev, chan->span,
 						     "Failed to set span\n");
 		}
 	}
Signature · 1 line
2.46.0
maintainer
#1 lore raw
Quoted 11 lines · On Fri, 4 Oct 2024 19:24:54 +0530 Mohammed Anees…

In the current implementation of the ltc2664_channel_config function, a variable named span is declared and initialized to 0, intended to capture the return value of the ltc2664_set_span function. However, the output of ltc2664_set_span is directly assigned to chan->span, leaving span unchanged. As a result, when the function later checks if (span < 0), this condition will never trigger an error since span remains 0, this flaw leads to ineffective error handling. The current patch resolves this issue by checking chan->span rather than span and also effectively removing span variable.

Good catch, but needs a Fixes tag.

Also, slight preference for using ret and assigning on success. Ideal is not to have side effects when an error occurs, though I'd imagine in this case we fail the probe so it doesn't matter and in such cases it's not a hard requirement.

Quoted 18 lines
---
 drivers/iio/dac/ltc2664.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/dac/ltc2664.c +1 -1
index 5be5345ac5c8..64c8a51aad81 100644
@@ -516,7 +516,7 @@ static int ltc2664_channel_config(struct ltc2664_state *st)
 	const struct ltc2664_chip_info *chip_info = st->chip_info;
 	struct device *dev = &st->spi->dev;
 	u32 reg, tmp[2], mspan;
-	int ret, span = 0;
+	int ret;
 
 	mspan = LTC2664_MSPAN_SOFTSPAN;
 	ret = device_property_read_u32(dev, "adi,manual-span-operation-config",

drivers/iio/dac/ltc2664.c static int ltc2664_channel_config(struct ltc2664_state *st) · line 583

Quoted 3 lines
drivers/iio/dac/ltc2664.c +0 -0
@@ -581,8 +581,8 @@ static int ltc2664_channel_config(struct ltc2664_state *st)
 		if (!ret && mspan == LTC2664_MSPAN_SOFTSPAN) {
 			chan->span = ltc2664_set_span(st, tmp[0] / 1000,
 						      tmp[1] / 1000, reg);

I'd slightly prefer

			ret =....
			if (ret < 0)
				...
			chan->span = ret;

etc

drivers/iio/dac/ltc2664.c static int ltc2664_channel_config(struct ltc2664_state *st) · line 594

Quoted 13 lines
drivers/iio/dac/ltc2664.c +2 -4
-			if (span < 0)
-				return dev_err_probe(dev, span,
+			if (chan->span < 0)
+				return dev_err_probe(dev, chan->span,
 						     "Failed to set span\n");
 		}
 
@@ -590,8 +590,8 @@ static int ltc2664_channel_config(struct ltc2664_state *st)
 						     tmp, ARRAY_SIZE(tmp));
 		if (!ret) {
 			chan->span = ltc2664_set_span(st, 0, tmp[1] / 1000, reg);
-			if (span < 0)
-				return dev_err_probe(dev, span,

similar down here.

Quoted 5 lines
drivers/iio/dac/ltc2664.c +2 -0
+			if (chan->span < 0)
+				return dev_err_probe(dev, chan->span,
 						     "Failed to set span\n");
 		}
 	}