Skip to content

Commit c4541a4

Browse files
gpouliosDonny9
authored andcommitted
libc/idr: Remove nodes from RB trees during destroy
idr_destroy() would loop over the removed and alloced RB tree nodes freeing them but not removing them from the trees. From the perspective of the RB tree those nodes would remain valid, while in fact, they were free memory, potentially reallocated for other purposes, or otherwise overwritten by the allocator with metadata. This would cause (seemingly random) memory corruption crashes triggered by the RB tree code trying to access link fields from the free'd nodes. Fix that by removing the nodes before freeing them. Signed-off-by: George Poulios <gpoulios@census-labs.com>
1 parent 95c9525 commit c4541a4

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

libs/libc/misc/lib_idr.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,13 @@ void idr_destroy(FAR struct idr_s *idr)
329329
nxmutex_lock(&idr->lock);
330330
RB_FOREACH_SAFE(node, idr_tree_s, &idr->removed, temp)
331331
{
332+
RB_REMOVE(idr_tree_s, &idr->removed, node);
332333
lib_free(node);
333334
}
334335

335336
RB_FOREACH_SAFE(node, idr_tree_s, &idr->alloced, temp)
336337
{
338+
RB_REMOVE(idr_tree_s, &idr->alloced, node);
337339
lib_free(node);
338340
}
339341

0 commit comments

Comments
 (0)