Skip to contentSkip to conversation

darray: Evaluate the index parameter only once in darray_remove()

PATCH In review Damien Grassart 2 participants 3 messages Aug 26, 2017 – Aug 27, 2017

lore
Share & tools
lore
#0 lore raw

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

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(-)
ccan/darray/darray.h +3 -2
index 8d47645b..6dd34f08 100644
@@ -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)
 
Signature · 6 lines
2.14.1

_______________________________________________
ccan mailing list
[email protected]
https://lists.ozlabs.org/listinfo/ccan
#1 lore raw
Quoted 6 lines · Damien Grassart

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(-)

Looks like a definite improvement, but I need a Signed-off-by line in order to apply it.

Also.. identifiers starting with _ are technically reserved for system use. You'd probably get away with it, but it's best to avoid them. I usually put _ after the name instead for this sort of thing.

Quoted 17 lines
ccan/darray/darray.h +3 -2
index 8d47645b..6dd34f08 100644
@@ -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)
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

_______________________________________________
ccan mailing list
[email protected]
https://lists.ozlabs.org/listinfo/ccan

#2 lore raw

Oops sorry I'll resend with the Signed-off-by.
For the underscores, I was trying to keep the same style as the rest of the code, but I can resend a patch with all the "__identifiers" switched to
"identifiers__"?

Quoted 35 lines · David Gibson
Quoted 6 lines · Damien Grassart

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(-)

Looks like a definite improvement, but I need a Signed-off-by line in order to apply it.

Also.. identifiers starting with _ are technically reserved for system use. You'd probably get away with it, but it's best to avoid them. I usually put _ after the name instead for this sort of thing.

ccan/darray/darray.h typedef darray(unsigned long) darray_ulong; · line 228

Quoted 11 lines
ccan/darray/darray.h +0 -2
index 8d47645b..6dd34f08 100644
@@ -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)); \

ccan/darray/darray.h typedef darray(unsigned long) darray_ulong;

Quoted 3 lines
ccan/darray/darray.h +3 -0
+     size_t __index = index; \
+     if (__index < arr.size-1)    \
+             memmove(&(arr).item[__index], &(arr).item[__index+1],

((arr).size-1-__index)*sizeof(*(arr).item)); \

ccan/darray/darray.h typedef darray(unsigned long) darray_ulong; · line 232

Quoted 3 lines
ccan/darray/darray.h +0 -0
      (arr).size--;  \
      } while(0)
 
Quoted 7 lines

--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_

                                | _way_ _around_!

http://www.ozlabs.org/~dgibson

_______________________________________________
ccan mailing list
[email protected]
https://lists.ozlabs.org/listinfo/ccan