[Stgt-devel] [PATCH 1/2] driver: fix array bounds checking

Pete Wyckoff pw
Thu May 29 19:51:34 CEST 2008


Gcc 4.3.0 noticed that the check on tgt_drivers[i] after
the loop could exceed the static bounds of the array.  Impressive.
Fix that by reordering the logic.

Signed-off-by: Pete Wyckoff <pw at osc.edu>
---
 usr/driver.c |   13 ++++++-------
 1 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/usr/driver.c b/usr/driver.c
index c555398..d886e86 100644
--- a/usr/driver.c
+++ b/usr/driver.c
@@ -49,12 +49,11 @@ int register_driver(struct tgt_driver *drv)
 {
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(tgt_drivers) && tgt_drivers[i]; i++)
-		;
+	for (i = 0; i < ARRAY_SIZE(tgt_drivers); i++)
+		if (tgt_drivers[i] == NULL) {
+			tgt_drivers[i] = drv;
+			return 0;
+		}
 
-	if (tgt_drivers[i])
-		return -1;
-
-	tgt_drivers[i] = drv;
-	return 0;
+	return -1;
 }
-- 
1.5.5.1




More information about the stgt mailing list