Skip to content

Commit 4e234da

Browse files
kikairoyajeremyd2019
authored andcommitted
[LLVM][Coverage][Unittest] Fix dangling reference in unittest (llvm#147118)
In loop of `writeAndReadCoverageRegions`, `OutputFunctions[I].Filenames` references to contents of `Filenames` after returning from `readCoverageRegions` but `Filenames` will be cleared in next call of `readCoverageRegions`, causes dangling reference. The lifetime of the contents of `Filenames` must be equal or longer than `OutputFunctions[I]`, thus it has been moved into `OutputFunctions[I]` (typed `OutputFunctionCoverageData`). (cherry picked from commit ca09801)
1 parent 17d70e8 commit 4e234da

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

llvm/unittests/ProfileData/CoverageMappingTest.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,18 @@ namespace {
6464
struct OutputFunctionCoverageData {
6565
StringRef Name;
6666
uint64_t Hash;
67+
std::vector<std::string> FilenamesStorage;
6768
std::vector<StringRef> Filenames;
6869
std::vector<CounterMappingRegion> Regions;
6970
std::vector<CounterExpression> Expressions;
7071

7172
OutputFunctionCoverageData() : Hash(0) {}
7273

7374
OutputFunctionCoverageData(OutputFunctionCoverageData &&OFCD)
74-
: Name(OFCD.Name), Hash(OFCD.Hash), Filenames(std::move(OFCD.Filenames)),
75-
Regions(std::move(OFCD.Regions)) {}
75+
: Name(OFCD.Name), Hash(OFCD.Hash),
76+
FilenamesStorage(std::move(OFCD.FilenamesStorage)),
77+
Filenames(std::move(OFCD.Filenames)), Regions(std::move(OFCD.Regions)) {
78+
}
7679

7780
OutputFunctionCoverageData(const OutputFunctionCoverageData &) = delete;
7881
OutputFunctionCoverageData &
@@ -135,7 +138,6 @@ struct InputFunctionCoverageData {
135138
struct CoverageMappingTest : ::testing::TestWithParam<std::tuple<bool, bool>> {
136139
bool UseMultipleReaders;
137140
StringMap<unsigned> Files;
138-
std::vector<std::string> Filenames;
139141
std::vector<InputFunctionCoverageData> InputFunctions;
140142
std::vector<OutputFunctionCoverageData> OutputFunctions;
141143

@@ -233,13 +235,11 @@ struct CoverageMappingTest : ::testing::TestWithParam<std::tuple<bool, bool>> {
233235

234236
void readCoverageRegions(const std::string &Coverage,
235237
OutputFunctionCoverageData &Data) {
236-
// We will re-use the StringRef in duplicate tests, clear it to avoid
237-
// clobber previous ones.
238-
Filenames.clear();
239-
Filenames.resize(Files.size() + 1);
238+
// +1 here since `Files` (filename to index map) uses 1-based index.
239+
Data.FilenamesStorage.resize(Files.size() + 1);
240240
for (const auto &E : Files)
241-
Filenames[E.getValue()] = E.getKey().str();
242-
ArrayRef<std::string> FilenameRefs = llvm::ArrayRef(Filenames);
241+
Data.FilenamesStorage[E.getValue()] = E.getKey().str();
242+
ArrayRef<std::string> FilenameRefs = llvm::ArrayRef(Data.FilenamesStorage);
243243
RawCoverageMappingReader Reader(Coverage, FilenameRefs, Data.Filenames,
244244
Data.Expressions, Data.Regions);
245245
EXPECT_THAT_ERROR(Reader.read(), Succeeded());

0 commit comments

Comments
 (0)