Skip to content

Conversation

@vitalybuka
Copy link
Collaborator

Don't rely on comparison to singular iterator, it's UB.

Fixes bot crashes after #164524.

Created using spr 1.3.7
@llvmbot
Copy link
Member

llvmbot commented Oct 27, 2025

@llvm/pr-subscribers-llvm-adt

Author: Vitaly Buka (vitalybuka)

Changes

Don't rely on comparison to singular iterator, it's UB.

Fixes bot crashes after #164524.


Full diff: https://github.com/llvm/llvm-project/pull/165299.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/RadixTree.h (+6-5)
diff --git a/llvm/include/llvm/ADT/RadixTree.h b/llvm/include/llvm/ADT/RadixTree.h
index 9e2ab9753d50c..87e2a3ebecc06 100644
--- a/llvm/include/llvm/ADT/RadixTree.h
+++ b/llvm/include/llvm/ADT/RadixTree.h
@@ -20,6 +20,7 @@
 #include <cstddef>
 #include <iterator>
 #include <list>
+#include <optional>
 #include <utility>
 #include <vector>
 
@@ -92,7 +93,7 @@ template <typename KeyType, typename T> class RadixTree {
     /// If this node does not have a value (i.e., it's an internal node that
     /// only serves as a path to other values), this iterator will be equal
     /// to default constructed `ContainerType::iterator()`.
-    typename ContainerType::iterator Value;
+    std::optional<typename ContainerType::iterator> Value;
 
     /// The first character of the Key. Used for fast child lookup.
     KeyValueType KeyFront;
@@ -215,7 +216,7 @@ template <typename KeyType, typename T> class RadixTree {
                                     KeyConstIteratorType{}};
 
     void findNextValid() {
-      while (Curr && Curr->Value == typename ContainerType::iterator())
+      while (Curr && !Curr->Value.has_value())
         advance();
     }
 
@@ -249,7 +250,7 @@ template <typename KeyType, typename T> class RadixTree {
   public:
     IteratorImpl() = default;
 
-    MappedType &operator*() const { return *Curr->Value; }
+    MappedType &operator*() const { return **Curr->Value; }
 
     IteratorImpl &operator++() {
       advance();
@@ -315,12 +316,12 @@ template <typename KeyType, typename T> class RadixTree {
     const value_type &NewValue = KeyValuePairs.emplace_front(
         std::move(Key), T(std::forward<Ts>(Args)...));
     Node &Node = findOrCreate(NewValue.first);
-    bool HasValue = Node.Value != typename ContainerType::iterator();
+    bool HasValue = Node.Value.has_value();
     if (!HasValue)
       Node.Value = KeyValuePairs.begin();
     else
       KeyValuePairs.pop_front();
-    return {Node.Value, !HasValue};
+    return {*Node.Value, !HasValue};
   }
 
   ///

@thurstond thurstond enabled auto-merge (squash) October 27, 2025 19:25
@thurstond thurstond disabled auto-merge October 27, 2025 19:30
@thurstond thurstond merged commit dce8252 into main Oct 27, 2025
7 of 11 checks passed
@thurstond thurstond deleted the users/vitalybuka/spr/radixtree-use-stdoptional-for-nodevalue branch October 27, 2025 19:30
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
Don't rely on comparison to singular iterator, it's UB.

Fixes bot crashes after
llvm#164524.
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
Don't rely on comparison to singular iterator, it's UB.

Fixes bot crashes after
llvm#164524.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
Don't rely on comparison to singular iterator, it's UB.

Fixes bot crashes after
llvm#164524.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants