Skip to content

Commit 7e7ab2f

Browse files
authored
Merge pull request #35345 from mikeash/safe-snapshots
[Runtime] Fix premature snapshot destruction in StableAddressConcurrentReadableHashMap::find.
2 parents bede9c8 + 13f84dd commit 7e7ab2f

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

include/swift/Runtime/Concurrent.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,13 @@ template <class ElemTy> struct ConcurrentReadableArray {
488488
~Snapshot() {
489489
Array->decrementReaders();
490490
}
491-
492-
const ElemTy *begin() { return Start; }
493-
const ElemTy *end() { return Start + Count; }
491+
492+
// These are marked as ref-qualified (the &) to make sure they can't be
493+
// called on temporaries, since the temporary would be destroyed before the
494+
// return value can be used, making it invalid.
495+
const ElemTy *begin() & { return Start; }
496+
const ElemTy *end() & { return Start + Count; }
497+
494498
size_t count() { return Count; }
495499
};
496500

@@ -974,7 +978,11 @@ struct ConcurrentReadableHashMap {
974978

975979
/// Search for an element matching the given key. Returns a pointer to the
976980
/// found element, or nullptr if no matching element exists.
977-
template <class KeyTy> const ElemTy *find(const KeyTy &key) {
981+
//
982+
// This is marked as ref-qualified (the &) to make sure it can't be called
983+
// on temporaries, since the temporary would be destroyed before the return
984+
// value can be used, making it invalid.
985+
template <class KeyTy> const ElemTy *find(const KeyTy &key) & {
978986
if (!Indices.Value || !ElementCount || !Elements)
979987
return nullptr;
980988
return ConcurrentReadableHashMap::find(key, Indices, ElementCount,
@@ -1181,7 +1189,8 @@ struct StableAddressConcurrentReadableHashMap
11811189
}
11821190

11831191
template <class KeyTy> ElemTy *find(const KeyTy &key) {
1184-
auto result = this->snapshot().find(key);
1192+
auto snapshot = this->snapshot();
1193+
auto result = snapshot.find(key);
11851194
if (!result)
11861195
return nullptr;
11871196
return result->Ptr;

0 commit comments

Comments
 (0)