Skip to content

Commit e78abc2

Browse files
authored
Merge pull request #40319 from bnbarham/loc-leaks
[AST] Prevent memory leak when allocating `ExternalSourceLocs`
2 parents bead157 + 50a73a3 commit e78abc2

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
@@ -653,9 +653,14 @@ const ExternalSourceLocs *Decl::getSerializedLocs() const {
653653
auto *Result = getASTContext().Allocate<ExternalSourceLocs>();
654654
Result->BufferID = BufferID;
655655
Result->Loc = ResolveLoc(RawLocs->Loc);
656-
for (auto &Range : RawLocs->DocRanges) {
657-
Result->DocRanges.emplace_back(ResolveLoc(Range.first), Range.second);
656+
657+
auto DocRanges = getASTContext().AllocateUninitialized<CharSourceRange>(RawLocs->DocRanges.size());
658+
for (auto I : indices(RawLocs->DocRanges)) {
659+
auto &Range = RawLocs->DocRanges[I];
660+
DocRanges[I] = CharSourceRange(ResolveLoc(Range.first), Range.second);
658661
}
662+
Result->DocRanges = DocRanges;
663+
659664
Context.setExternalSourceLocs(this, Result);
660665
return Result;
661666
}

0 commit comments

Comments
 (0)