Skip to content

Commit dce8252

Browse files
authored
[RadixTree] Use std::optional for Node::Value (#165299)
Don't rely on comparison to singular iterator, it's UB. Fixes bot crashes after #164524.
1 parent 9abae17 commit dce8252

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

llvm/include/llvm/ADT/RadixTree.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cstddef>
2121
#include <iterator>
2222
#include <list>
23+
#include <optional>
2324
#include <utility>
2425
#include <vector>
2526

@@ -92,7 +93,7 @@ template <typename KeyType, typename T> class RadixTree {
9293
/// If this node does not have a value (i.e., it's an internal node that
9394
/// only serves as a path to other values), this iterator will be equal
9495
/// to default constructed `ContainerType::iterator()`.
95-
typename ContainerType::iterator Value;
96+
std::optional<typename ContainerType::iterator> Value;
9697

9798
/// The first character of the Key. Used for fast child lookup.
9899
KeyValueType KeyFront;
@@ -215,7 +216,7 @@ template <typename KeyType, typename T> class RadixTree {
215216
KeyConstIteratorType{}};
216217

217218
void findNextValid() {
218-
while (Curr && Curr->Value == typename ContainerType::iterator())
219+
while (Curr && !Curr->Value.has_value())
219220
advance();
220221
}
221222

@@ -249,7 +250,7 @@ template <typename KeyType, typename T> class RadixTree {
249250
public:
250251
IteratorImpl() = default;
251252

252-
MappedType &operator*() const { return *Curr->Value; }
253+
MappedType &operator*() const { return **Curr->Value; }
253254

254255
IteratorImpl &operator++() {
255256
advance();
@@ -315,12 +316,12 @@ template <typename KeyType, typename T> class RadixTree {
315316
const value_type &NewValue = KeyValuePairs.emplace_front(
316317
std::move(Key), T(std::forward<Ts>(Args)...));
317318
Node &Node = findOrCreate(NewValue.first);
318-
bool HasValue = Node.Value != typename ContainerType::iterator();
319+
bool HasValue = Node.Value.has_value();
319320
if (!HasValue)
320321
Node.Value = KeyValuePairs.begin();
321322
else
322323
KeyValuePairs.pop_front();
323-
return {Node.Value, !HasValue};
324+
return {*Node.Value, !HasValue};
324325
}
325326

326327
///

0 commit comments

Comments
 (0)