Skip to contentSkip to conversation

i2c: stm32f7: prevent array underflow in stm32f7_get_lower_rate()

PATCH Applied Dan Carpenter 3 participants 3 messages Apr 29, 2020 – Apr 30, 2020

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

PATCH 1/1 i2c: stm32f7: prevent array underflow in stm32f7_get_lower_rate()

We want to break with "i" set to zero whether we find the rate we want or not. In the current code, if we don't find the rate we want then it exits the loop with "i" set to -1 and results in an array underflow.

Fixes 09cc9a3bce91 ("i2c: stm32f7: allows for any bus frequency")
---
 drivers/i2c/busses/i2c-stm32f7.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-stm32f7.c +1 -1
index 9c9e10ea91991..bff3479fe122a 100644
@@ -607,7 +607,7 @@ static u32 stm32f7_get_lower_rate(u32 rate)
 {
 	int i = ARRAY_SIZE(stm32f7_i2c_specs);
 
-	while (i--)
+	while (--i)
 		if (stm32f7_i2c_specs[i].rate < rate)
 			break;
 
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw

Hi Dan,

indeed, thanks a lot.

Alain

Quoted 26 lines · Dan Carpenter

We want to break with "i" set to zero whether we find the rate we want or not. In the current code, if we don't find the rate we want then it exits the loop with "i" set to -1 and results in an array underflow.

Fixes 09cc9a3bce91 ("i2c: stm32f7: allows for any bus frequency")
---
 drivers/i2c/busses/i2c-stm32f7.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-stm32f7.c +1 -1
index 9c9e10ea91991..bff3479fe122a 100644
@@ -607,7 +607,7 @@ static u32 stm32f7_get_lower_rate(u32 rate)
 {
 	int i = ARRAY_SIZE(stm32f7_i2c_specs);
 
-	while (i--)
+	while (--i)
 		if (stm32f7_i2c_specs[i].rate < rate)
 			break;
 
Signature · 1 line
2.26.2
Applied
#2 lore raw
In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter

We want to break with "i" set to zero whether we find the rate we want or not. In the current code, if we don't find the rate we want then it exits the loop with "i" set to -1 and results in an array underflow.

Fixes 09cc9a3bce91 ("i2c: stm32f7: allows for any bus frequency")

Applied to for-next, thanks!