Error checking for VECTOR_XXX defines
authorHannes Reinecke <hare@suse.de>
Mon, 26 Jan 2009 12:01:33 +0000 (13:01 +0100)
committerChristophe Varoqui <christophe.varoqui@free.fr>
Tue, 21 Apr 2009 23:26:32 +0000 (01:26 +0200)
Christophe doesn't believe in error checking. I, however, have
been tripping over them all the time.

References: bnc#469269

Signed-off-by: Hannes Reinecke <hare@suse.de>
libmultipath/vector.h

index aa9e134..59cfd27 100644 (file)
@@ -31,9 +31,9 @@ struct _vector {
 typedef struct _vector *vector;
 
 #define VECTOR_DEFAULT_SIZE 1
-#define VECTOR_SLOT(V,E) ((V)->slot[(E)])
-#define VECTOR_SIZE(V)   ((V)->allocated)
-#define VECTOR_LAST_SLOT(V)   ((V)->slot[((V)->allocated - 1)])
+#define VECTOR_SLOT(V,E) (((V) && (E) < (V)->allocated) ? (V)->slot[(E)] : NULL)
+#define VECTOR_SIZE(V)   ((V) ? (V)->allocated : 0)
+#define VECTOR_LAST_SLOT(V)   (((V) && (V)->allocated) ? (V)->slot[((V)->allocated - 1)] : NULL)
 
 #define vector_foreach_slot(v,p,i) \
        for (i = 0; (v) && i < (v)->allocated && ((p) = (v)->slot[i]); i++)