From b1f8c5879f395e1049b09c0608dc59e11768d93f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 29 Nov 2022 14:40:05 +0100 Subject: [PATCH] LibIDL: Fix bug where entire EffectiveOverloadSet was erased There was a funny bug here: by storing the "last matched item" as a pointer, and then using Vector::remove_all_matching() to remove all items that didn't have that exact address, we would end up removing everything unless the last item matched was the very first item. (This happened because every time an item was removed from the vector, the remaining contents shift one step towards the start of the vector, affecting item addresses.) This patch fixes the issue by storing the last match as an index. --- Userland/Libraries/LibIDL/Types.cpp | 6 +++--- Userland/Libraries/LibIDL/Types.h | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibIDL/Types.cpp b/Userland/Libraries/LibIDL/Types.cpp index 41eef5e817..4e49f71d9d 100644 --- a/Userland/Libraries/LibIDL/Types.cpp +++ b/Userland/Libraries/LibIDL/Types.cpp @@ -214,9 +214,9 @@ int EffectiveOverloadSet::distinguishing_argument_index() void EffectiveOverloadSet::remove_all_other_entries() { - m_items.remove_all_matching([this](auto const& item) { - return &item != m_last_matching_item; - }); + Vector new_items; + new_items.append(m_items[*m_last_matching_item_index]); + m_items = move(new_items); } } diff --git a/Userland/Libraries/LibIDL/Types.h b/Userland/Libraries/LibIDL/Types.h index e06c8f2a8f..ba2a258eef 100644 --- a/Userland/Libraries/LibIDL/Types.h +++ b/Userland/Libraries/LibIDL/Types.h @@ -432,13 +432,14 @@ public: template bool has_overload_with_matching_argument_at_index(size_t index, Matches matches) { - for (auto const& item : m_items) { + for (size_t i = 0; i < m_items.size(); ++i) { + auto const& item = m_items[i]; if (matches(item.types[index], item.optionality_values[index])) { - m_last_matching_item = &item; + m_last_matching_item_index = i; return true; } } - m_last_matching_item = nullptr; + m_last_matching_item_index = {}; return false; } @@ -449,7 +450,7 @@ private: Vector m_items; size_t m_argument_count; - Item const* m_last_matching_item { nullptr }; + Optional m_last_matching_item_index; }; }