Skip to contentSkip to conversation

iio: adc: mcp3911: fix sizeof() vs ARRAY_SIZE() bug

PATCH Applied Dan Carpenter 3 participants 3 messages Sep 26, 2022 – Oct 2, 2022

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

PATCH 1/1 iio: adc: mcp3911: fix sizeof() vs ARRAY_SIZE() bug

This code uses sizeof() instead of ARRAY_SIZE() so it reads beyond the end of the mcp3911_osr_table[] array.

Fixes 6d965885f4ea ("iio: adc: mcp3911: add support for oversampling ratio")
---
 drivers/iio/adc/mcp3911.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/mcp3911.c +1 -1
index b35fd2c9c3c0..015a9ffdb26a 100644
@@ -248,7 +248,7 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
 		break;
 
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
-		for (int i = 0; i < sizeof(mcp3911_osr_table); i++) {
+		for (int i = 0; i < ARRAY_SIZE(mcp3911_osr_table); i++) {
 			if (val == mcp3911_osr_table[i]) {
 				val = FIELD_PREP(MCP3911_CONFIG_OSR, i);
 				ret = mcp3911_update(adc, MCP3911_REG_CONFIG, MCP3911_CONFIG_OSR,
Signature · 1 line
2.35.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi,

Good catch.
Too bad I missed it.

/Marcus

Quoted 5 lines · Dan Carpenter

This code uses sizeof() instead of ARRAY_SIZE() so it reads beyond the end of the mcp3911_osr_table[] array.

Fixes 6d965885f4ea ("iio: adc: mcp3911: add support for oversampling ratio")
Quoted 20 lines
---
 drivers/iio/adc/mcp3911.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/mcp3911.c +1 -1
index b35fd2c9c3c0..015a9ffdb26a 100644
@@ -248,7 +248,7 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
 		break;
 
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
-		for (int i = 0; i < sizeof(mcp3911_osr_table); i++) {
+		for (int i = 0; i < ARRAY_SIZE(mcp3911_osr_table); i++) {
 			if (val == mcp3911_osr_table[i]) {
 				val = FIELD_PREP(MCP3911_CONFIG_OSR, i);
 				ret = mcp3911_update(adc, MCP3911_REG_CONFIG, MCP3911_CONFIG_OSR,
Signature · 1 line
2.35.1
maintainer Applied
#2 lore raw
Quoted 7 lines · On Mon, 26 Sep 2022 13:18:58 +0200 Marcus Folkesson…

Hi,

Good catch.
Too bad I missed it.

/Marcus

Quoted 8 lines
Quoted 5 lines · Dan Carpenter

This code uses sizeof() instead of ARRAY_SIZE() so it reads beyond the end of the mcp3911_osr_table[] array.

Fixes 6d965885f4ea ("iio: adc: mcp3911: add support for oversampling ratio")

Applied to the fixes-togreg branch of iio.git, but I'm not going to push that tree out until Linus takes the char-misc-next pull request as otherwise I'll make a mess of linux-next as the trees will effectively merge out of normal order.

Jonathan

Quoted 21 lines
Quoted 21 lines
---
 drivers/iio/adc/mcp3911.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/mcp3911.c +1 -1
index b35fd2c9c3c0..015a9ffdb26a 100644
@@ -248,7 +248,7 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
 		break;
 
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
-		for (int i = 0; i < sizeof(mcp3911_osr_table); i++) {
+		for (int i = 0; i < ARRAY_SIZE(mcp3911_osr_table); i++) {
 			if (val == mcp3911_osr_table[i]) {
 				val = FIELD_PREP(MCP3911_CONFIG_OSR, i);
 				ret = mcp3911_update(adc, MCP3911_REG_CONFIG, MCP3911_CONFIG_OSR,
Signature · 1 line
2.35.1