Skip to contentSkip to conversation

libmultipath: make vector_foreach_slot_backwards work as expected

PATCH In review Benjamin Marzinski 2 participants 3 messages May 24, 2019 – May 28, 2019 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

PATCH 1/1 libmultipath: make vector_foreach_slot_backwards work as expected

All of the code that uses vector_foreach_slot_backwards() treats "i" as the index of the entry "p", but the way it was coded, that wasn't the case. "i" was the number of the entry counting from 1, not 0.

---
 libmultipath/vector.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/vector.h +1 -1
index 41d2b896..344dffd5 100644
@@ -40,7 +40,7 @@ typedef struct _vector *vector;
 #define vector_foreach_slot_after(v,p,i) \
 	for (; (v) && i < VECTOR_SIZE(v) && ((p) = (v)->slot[i]); i++)
 #define vector_foreach_slot_backwards(v,p,i) \
-	for (i = VECTOR_SIZE(v); i > 0 && ((p) = (v)->slot[i-1]); i--)
+	for (i = VECTOR_SIZE(v) - 1; (int)i >= 0 && ((p) = (v)->slot[i]); i--)
 
 #define identity(x) (x)
 /*
Signature · 1 line
2.17.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Benjamin Marzinski

All of the code that uses vector_foreach_slot_backwards() treats "i" as
the index of the entry "p", but the way it was coded, that wasn't the case. "i" was the number of the entry counting from 1, not 0.

---
 libmultipath/vector.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Let's add that there was only one caller that actually used "i"("multipath -W"). So the harm done by this bug was not as bad as one might think.

author maintainer
#2 lore raw
Quoted 12 lines · Martin Wilck
Quoted 10 lines · Benjamin Marzinski

All of the code that uses vector_foreach_slot_backwards() treats "i" as
the index of the entry "p", but the way it was coded, that wasn't the case. "i" was the number of the entry counting from 1, not 0.

---
 libmultipath/vector.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 6 lines

Let's add that there was only one caller that actually used "i"("multipath -W"). So the harm done by this bug was not as bad as one might think.

Actually, it caused _find_controllers() to delete the wrong slot from map->pgvec, which caused a crash on a future _find_controllers() call because we don't check for a NULL path between

path = nvme_pg_to_path(pg);

and

path->seen = false;

Since there should never be a NULL path, I don't think we need to add that check, but this happens in more than multipath -W. We use "i" in _cleanup_foreign(), cleanup_nvme_map(), _delete_all(), and _find_controllers().

-Ben