fluent / cfl

Tiny library for data structures management, call it c:\ floppy
Apache License 2.0
0 stars 6 forks source link

cfl_array: fix memory overlap when removing from array by index. #42

Closed pwhelan closed 7 months ago

pwhelan commented 7 months ago

This code will fail, at the very least when using ASAn:

    cfl_variant_destroy(array->entries[position]);

    if (position != array->entry_count - 1) {
        memcpy(&array->entries[position],
               &array->entries[position + 1],
               sizeof(void *) * (array->entry_count - (position + 1)));
    }
    else {
        array->entries[position] = NULL;

This is because you will inevitably be copying over the remaining values on top of the value being removed.

This fix just replaces memcpy for memmove which allows for src and dst to overlap.

edsiper commented 7 months ago

thanks