Tessil / hat-trie

C++ implementation of a fast and memory efficient HAT-trie
MIT License
795 stars 114 forks source link

Segmentation fault when map is iterated after erase prefix #57

Closed kishorenc closed 3 months ago

kishorenc commented 6 months ago

Here's a minimally reproducible example that causes a crash:

#include <iostream>
#include <tsl/htrie_map.h>

int main() {
    tsl::htrie_map<char, std::string> data_map;
    std::cout << "Inserting items into map..." << std::endl;
    data_map.emplace("data", "foo");

    for(size_t i = 0; i < 30000; i++) {
        std::string key = "data." + std::to_string(i);
        data_map.insert(key, "foo");
    }

    size_t count = 0;

    std::cout << "Erase prefix..." << std::endl;
    data_map.erase_prefix("data.");

    std::cout << "Start iteration..." << std::endl;

    for(auto it = data_map.begin(); it != data_map.end(); it++) {
        count++;
    }

    std::cout << "End iteration, count: " << count << std::endl;
    return 0;
}
Tessil commented 3 months ago

Hi,

Thank you for the report and sorry for the delay.

The https://github.com/Tessil/hat-trie/commit/ad6d85cfa239b26e2ce5dbfe13a51a1c581799a4 commit should fix the issue, if you could try it out.