Skip to contentSkip to conversation

iio: light: opt4001: Fix a wrong array iteration limit

PATCH In review Jonathan Cameron 2 participants 2 messages Aug 7, 2023 – Aug 8, 2023 IIO

lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

PATCH 1/1 iio: light: opt4001: Fix a wrong array iteration limit

From: Jonathan Cameron <[email protected]>

In practice it's unlikely anyone would try to set the integration time to 0 which isn't in the available list and if they did then they would get index 12 which whilst reserved on the device fits in the field. However a compiler might get half way through this reasoning and that might be the cause of

Quoted 2 lines
Quoted 1 line
Quoted 1 line

drivers/iio/light/opt4001.c:215:9: error: call to '__compiletime_assert_355' declared with 'error' attribute: FIELD_PREP: value too large for the field

     215 |         reg |= FIELD_PREP(OPT4001_CTRL_CONV_TIME_MASK, chip->int_time);

Even if this isn't the cause, it looks like a bug to me.

Fixes 9a9608418292 ("iio: light: Add support for TI OPT4001 light sensor")
Cc Stefan Windfeldt-Prytz <[email protected]>
---
 drivers/iio/light/opt4001.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/light/opt4001.c +1 -1
index 502946bf9f94..83f978706758 100644
@@ -137,7 +137,7 @@ static int opt4001_als_time_to_index(const u32 als_integration_time)
 {
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(opt4001_int_time_available); i++) {
+	for (i = 0; i < ARRAY_SIZE(opt4001_int_time_available) / 2; i++) {
 		if (als_integration_time == opt4001_int_time_available[i][1])
 			return i;
 	}
Signature · 1 line
2.41.0
#1 lore raw
Quoted 11 lines · On Mon, 7 Aug 2023 20:50:43 +0100 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

In practice it's unlikely anyone would try to set the integration time to 0 which isn't in the available list and if they did then they would get index 12 which whilst reserved on the device fits in the field. However a compiler might get half way through this reasoning and that might be the cause of

Quoted 2 lines
Quoted 1 line
Quoted 1 line

drivers/iio/light/opt4001.c:215:9: error: call to '__compiletime_assert_355' declared with 'error' attribute: FIELD_PREP: value too large for the field

     215 |         reg |= FIELD_PREP(OPT4001_CTRL_CONV_TIME_MASK, chip->int_time);  
Quoted 7 lines

Even if this isn't the cause, it looks like a bug to me.

Fixes 9a9608418292 ("iio: light: Add support for TI OPT4001 light sensor")
Cc Stefan Windfeldt-Prytz <[email protected]>

Oops. Clearly half asleep last night. This is garbage so I'm back to the drawing board on what is causing clang to believe int_time can be bigger than 4 bits.

Any suggestions welcome!

Quoted 17 lines
---
 drivers/iio/light/opt4001.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/light/opt4001.c +1 -1
index 502946bf9f94..83f978706758 100644
@@ -137,7 +137,7 @@ static int opt4001_als_time_to_index(const u32 als_integration_time)
 {
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(opt4001_int_time_available); i++) {
+	for (i = 0; i < ARRAY_SIZE(opt4001_int_time_available) / 2; i++) {
 		if (als_integration_time == opt4001_int_time_available[i][1])
 			return i;
 	}