Browse Source

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.
Andreas Kling 2 years ago
parent
commit
b1f8c5879f
2 changed files with 8 additions and 7 deletions
  1. 3 3
      Userland/Libraries/LibIDL/Types.cpp
  2. 5 4
      Userland/Libraries/LibIDL/Types.h

+ 3 - 3
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<Item> new_items;
+    new_items.append(m_items[*m_last_matching_item_index]);
+    m_items = move(new_items);
 }
 
 }

+ 5 - 4
Userland/Libraries/LibIDL/Types.h

@@ -432,13 +432,14 @@ public:
     template<typename Matches>
     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<Item> m_items;
     size_t m_argument_count;
 
-    Item const* m_last_matching_item { nullptr };
+    Optional<size_t> m_last_matching_item_index;
 };
 
 }