[ccan] [PATCH 2/2] darray: Evaluate the index parameter only once in darray_remove()

Damien Grassart damien at grassart.com
Sun Aug 27 02:26:52 AEST 2017


To be consistent with the rest of the code, the index paramater should
not be evaluated multiple times. Calling this with rand() % arr.size
would otherwise generally segfault.
---
 ccan/darray/darray.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/ccan/darray/darray.h b/ccan/darray/darray.h
index 8d47645b..6dd34f08 100644
--- a/ccan/darray/darray.h
+++ b/ccan/darray/darray.h
@@ -224,8 +224,9 @@ typedef darray(unsigned long)  darray_ulong;
 #define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL)
 /* Warning, slow: Requires copying all elements after removed item. */
 #define darray_remove(arr, index) do { \
-	if (index < arr.size-1)    \
-		memmove(&(arr).item[index], &(arr).item[index+1], ((arr).size-1-index)*sizeof(*(arr).item)); \
+	size_t __index = index; \
+	if (__index < arr.size-1)    \
+		memmove(&(arr).item[__index], &(arr).item[__index+1], ((arr).size-1-__index)*sizeof(*(arr).item)); \
 	(arr).size--;  \
 	} while(0)
 
-- 
2.14.1



More information about the ccan mailing list