Skip to content

Commit 50a73a3

Browse files
committed
[AST] Prevent memory leak when allocating ExternalSourceLocs
c763ab5 fixed an issue in `getSerializedLocs` where it never actually cached its result (and thus always allocated a new `CachedExternalSourceLocs`). Unfortunately it missed a leak that could occur when `DocRanges` grows beyond its initial size of 4. Allocate `DocRanges` upfront in the `ASTContext` as well in order to prevent this leak. Resolves rdar://85472403.
1 parent 7be6058 commit 50a73a3

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

include/swift/Basic/BasicSourceInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct ExternalSourceLocs {
5050

5151
unsigned BufferID = 0;
5252
SourceLoc Loc;
53-
SmallVector<CharSourceRange, 4> DocRanges;
53+
ArrayRef<CharSourceRange> DocRanges;
5454
};
5555

5656
class BasicSourceFileInfo {

lib/AST/Decl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,14 @@ const ExternalSourceLocs *Decl::getSerializedLocs() const {
652652
auto *Result = getASTContext().Allocate<ExternalSourceLocs>();
653653
Result->BufferID = BufferID;
654654
Result->Loc = ResolveLoc(RawLocs->Loc);
655-
for (auto &Range : RawLocs->DocRanges) {
656-
Result->DocRanges.emplace_back(ResolveLoc(Range.first), Range.second);
655+
656+
auto DocRanges = getASTContext().AllocateUninitialized<CharSourceRange>(RawLocs->DocRanges.size());
657+
for (auto I : indices(RawLocs->DocRanges)) {
658+
auto &Range = RawLocs->DocRanges[I];
659+
DocRanges[I] = CharSourceRange(ResolveLoc(Range.first), Range.second);
657660
}
661+
Result->DocRanges = DocRanges;
662+
658663
Context.setExternalSourceLocs(this, Result);
659664
return Result;
660665
}

0 commit comments

Comments
 (0)