Skip to contentSkip to conversation

agp: check for index out of range before accessing maxes_table[index]

PATCH New Colin King 1 participant 1 message Sep 6, 2018

Subsystems (2)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 agp: check for index out of range before accessing maxes_table[index]

From: Colin Ian King <[email protected]>

Currently maxes_table[index] is accessed before index is checked to be out of range. Fix this by performing the out of range check on index first. Also don't check against a hard-coded maximum value but use the array size instead.

Detected by static analysis with cppcheck:
"Array index 'index' is used before limits check"

---
 drivers/char/agp/backend.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/char/agp/backend.c +2 -1
index 38ffb281df97..4e07f01833a2 100644
@@ -121,7 +121,8 @@ static int agp_find_max(void)
 #endif
 	index = 1;
 
-	while ((memory > maxes_table[index].mem) && (index < 8))
+	while ((index < ARRAY_SIZE(maxes_table) - 1) &&
+	       (memory > maxes_table[index].mem))
 		index++;
 
 	result = maxes_table[index - 1].agp +
Signature · 1 line
2.17.1