-
Notifications
You must be signed in to change notification settings - Fork 949
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
Conversation
Signed-off-by: kostas <kostas@dragonflydb.io>
for (auto it = begin(); it != end(); ++it) { | ||
V* ptr = &(*it).second; | ||
std::allocator_traits<decltype(alloc_)>::destroy(alloc_, ptr); | ||
alloc_.deallocate(ptr, 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really forgot this 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻
There was a problem hiding this comment.
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))}; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
The main problem is that
find(std::string_view key)
returns aFindIterator
which contains thekey
that was passed as argument in the call tofind
and not the actualkey contained and allocated within rax
and is easy dangle: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
.