`pw_map_insert_at()` does not update free list which may result in lost elements
If pw_map_insert_at
is used to insert to an id which was previously removed, then the free list may become corrupted:
#include <stdio.h>
#include <pipewire/map.h>
static int f(void *data, void *context)
{
(void) context;
printf("%s\n", data);
return 0;
}
int main(void) {
static const char * const strs[] = { "A", "B", "C" };
struct pw_map m;
pw_map_init(&m, 16, sizeof(strs[0]));
uint32_t id = pw_map_insert_new(&m, strs[0]);
pw_map_remove(&m, id);
pw_map_insert_at(&m, id, strs[1]);
pw_map_insert_new(&m, strs[2]);
pw_map_for_each(&m, f, NULL);
pw_map_clear(&m);
return 0;
}
The code above prints:
C
instead of
B
C
since the index of the previously inserted B
hasn't been removed from the free list, in this case, m.free_list == id
even after the pw_map_insert_at()
call, thus the next pw_map_insert_new()
will override the data.
This limitation doesn't seem to be documented anywhere, am I missing something? I suppose this is the expected behaviour?