Skip to contentSkip to conversation

vector: add lower bound check of E in VECTOR_SLOT

PATCH In review Zhiqiang Liu 2 participants 2 messages Aug 3, 2020 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vector: add lower bound check of E in VECTOR_SLOT

In VECTOR_SLOT(V, E), we add the lower bound check of E. E cannot be less than zero.

---
 libmultipath/vector.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/vector.h +1 -1
index e16ec461..cdb94b24 100644
@@ -32,7 +32,7 @@ typedef struct _vector *vector;
 
 #define VECTOR_DEFAULT_SIZE 1
 #define VECTOR_SIZE(V)   ((V) ? ((V)->allocated) / VECTOR_DEFAULT_SIZE : 0)
-#define VECTOR_SLOT(V,E) (((V) && (E) < VECTOR_SIZE(V)) ? (V)->slot[(E)] : NULL)
+#define VECTOR_SLOT(V,E) (((V) && (E) < VECTOR_SIZE(V) && (E) >= 0) ? (V)->slot[(E)] : NULL)
 #define VECTOR_LAST_SLOT(V)   (((V) && VECTOR_SIZE(V) > 0) ? (V)->slot[(VECTOR_SIZE(V) - 1)] : NULL)
 
 #define vector_foreach_slot(v,p,i) \
Signature · 1 line
2.24.0.windows.2
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Zhiqiang Liu

In VECTOR_SLOT(V, E), we add the lower bound check of E. E cannot be less than zero.

Quoted 20 lines
---
 libmultipath/vector.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/vector.h +1 -1
index e16ec461..cdb94b24 100644
@@ -32,7 +32,7 @@ typedef struct _vector *vector;
 
 #define VECTOR_DEFAULT_SIZE 1
 #define VECTOR_SIZE(V)   ((V) ? ((V)->allocated) / VECTOR_DEFAULT_SIZE : 0)
-#define VECTOR_SLOT(V,E) (((V) && (E) < VECTOR_SIZE(V)) ? (V)->slot[(E)] : NULL)
+#define VECTOR_SLOT(V,E) (((V) && (E) < VECTOR_SIZE(V) && (E) >= 0) ? (V)->slot[(E)] : NULL)
 #define VECTOR_LAST_SLOT(V)   (((V) && VECTOR_SIZE(V) > 0) ? (V)->slot[(VECTOR_SIZE(V) - 1)] : NULL)
 
 #define vector_foreach_slot(v,p,i) \
Signature · 1 line
2.24.0.windows.2