[vlc-devel] [PATCH] vector: fix conflict between index and size in swap_remove

Alexandre Janniaux ajanni at videolabs.io
Fri Aug 30 16:42:12 CEST 2019


Undefined behaviour and warnings can happen when calling
vlc_vector_swap_remove with the array size as parameter, so as to
remove the last item for example.
---
 include/vlc_vector.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/include/vlc_vector.h b/include/vlc_vector.h
index 76fc2822fd..515e7a4035 100644
--- a/include/vlc_vector.h
+++ b/include/vlc_vector.h
@@ -591,7 +591,10 @@ vlc_vector_move_(char *array, size_t index, size_t count, size_t target)
  * \param index the index of item to remove
  */
 #define vlc_vector_swap_remove(pv, index) \
-    (pv)->data[index] = (pv)->data[--(pv)->size]
+    do { \
+        (pv)->data[index] = (pv)->data[(pv)->size-1]; \
+        (pv)->size--; \
+    } while(0)

 /**
  * Return the index of an item.
--
2.23.0


More information about the vlc-devel mailing list