common: add helper to remove a range of elements
What changed, and why it matters
This commit adds a new internal helper function for removing a contiguous block of items from a memory array. It is a straightforward utility addition with no bug fix, no change to existing behavior, and no security relevance visible in the commit itself.
No security action required. Review future commits that adopt this helper to ensure callers pass correct position/count values, since misuse could truncate or corrupt array state.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces tal_arr_remove_range() and its implementation tal_arr_remove_range_() in common/utils.c/h. The helper removes count elements starting at position from a tal-allocated array by memmove() and tal_resize(). It follows the same pattern as the existing tal_arr_remove() helper and includes an assertion that the requested range fits within the array length. No callers are added or modified in this commit.
Changed components
common/utils.ccommon/utils.hInspect captured patch +22 / −0
diff --git a/common/utils.c b/common/utils.c
index 1a079900..0bd7a72c 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -147,6 +147,17 @@ void tal_arr_remove_(void *p, size_t elemsize, size_t n)
tal_resize((char **)p, len - elemsize);
}
+void tal_arr_remove_range_(void *p, size_t position, size_t chunk_size)
+{
+ // p is a pointer-to-pointer for tal_resize.
+ char *objp = *(char **)p;
+ size_t len = tal_bytelen(objp);
+ assert(chunk_size + position <= len);
+ memmove(objp + position, objp + position + chunk_size,
+ len - (chunk_size + position));
+ tal_resize((char **)p, len - chunk_size);
+}
+
static void tal_arr_append_bytes(void *p, const void *append, size_t bytes)
{
void **pptr = p;
diff --git a/common/utils.h b/common/utils.h
index 560ec9f5..ac719d0c 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -86,6 +86,17 @@ bool tal_arr_eq_(const void *a, const void *b, size_t unused);
#define tal_arr_remove(p, n) tal_arr_remove_((p), sizeof(**p), (n))
void tal_arr_remove_(void *p, size_t elemsize, size_t n);
+/**
+ * Remove a range of element from an array
+ *
+ * This will shift the elements past the removed elements, changing
+ * their position in memory, so only use this for simple arrays.
+ */
+#define tal_arr_remove_range(p, position, count) \
+ tal_arr_remove_range_((p), sizeof(**p) * (position), \
+ sizeof(**p) * (count))
+void tal_arr_remove_range_(void *p, size_t position, size_t chunk_size);
+
/**
* Insert an element in an array
*/
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.