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

Alexandre Janniaux git at videolan.org
Mon Sep 2 10:07:44 CEST 2019


vlc | branch: master | Alexandre Janniaux <ajanni at videolabs.io> | Fri Aug 30 16:42:12 2019 +0200| [5b8f1a76b74587cb7f6eabb28e7f0e8dcda61ae9] | committer: Thomas Guillem

vector: fix conflict between index and size in swap_remove

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.

Signed-off-by: Thomas Guillem <thomas at gllm.fr>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5b8f1a76b74587cb7f6eabb28e7f0e8dcda61ae9
---

 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.



More information about the vlc-commits mailing list