Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace string_view with string in RaxTreeMap::FindIterator #3982

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

kostasrim
Copy link
Contributor

@kostasrim kostasrim commented Oct 24, 2024

The main problem is that find(std::string_view key) returns a FindIterator which contains the key that was passed as argument in the call to find and not the actual key contained and allocated within rax and is easy dangle:

  FindIterator res = find(std::string("some_key")); 

res now contains a dangling pointer because the internal string_view now points to the temporary (std::string("some_key")) which got deallocated at the end of the expression above.

The simplest solution is to make FindIterator own the key. (yes it does copy which is not great but it avoids dangling pointers).

We can always optimize the copy later.

Also fixes a memory leak in the destructor: we need to iterate over all the available children in the rax tree and delete the data we allocated before we call raxFree.

  • fixes dangling pointer caused by find()
  • fixes memory leak in the destructor

Signed-off-by: kostas <kostas@dragonflydb.io>
@kostasrim kostasrim marked this pull request as ready for review October 24, 2024 12:08
@kostasrim kostasrim self-assigned this Oct 24, 2024
Comment on lines +96 to +100
for (auto it = begin(); it != end(); ++it) {
V* ptr = &(*it).second;
std::allocator_traits<decltype(alloc_)>::destroy(alloc_, ptr);
alloc_.deallocate(ptr, 1);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really forgot this 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -113,7 +119,8 @@ template <typename V> struct RaxTreeMap {

FindIterator find(std::string_view key) const {
if (void* ptr = raxFind(tree_, to_key_ptr(key), key.size()); ptr != raxNotFound)
return FindIterator{std::pair<std::string_view, V&>(key, *reinterpret_cast<V*>(ptr))};
return FindIterator{std::pair<std::string, V&>(std::string(key), *reinterpret_cast<V*>(ptr))};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's dangerous, but it also means every lookup is a copy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we already leaked in the tests 😄 I will figure out something and push it. I will merge this and follow up

@kostasrim kostasrim merged commit 79963f9 into main Oct 24, 2024
13 checks passed
@kostasrim kostasrim deleted the kpr2 branch October 24, 2024 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants