llvm.org GIT mirror llvm / 8288f17
[llvm-profdata] Fix use-after-free from discarded MemoryBuffer (NFC) Thanks to Justin Bogner for pointing this out! Caught by ASAN. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271748 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 3 years ago
1 changed file(s) with 15 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
222222 return WeightedFile(FileName, Weight);
223223 }
224224
225 static void parseInputFilenamesFile(const StringRef &InputFilenamesFile,
226 WeightedFileVector &WFV) {
225 static std::unique_ptr
226 parseInputFilenamesFile(const StringRef &InputFilenamesFile,
227 WeightedFileVector &WFV) {
227228 if (InputFilenamesFile == "")
228 return;
229
230 auto Buf = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
231 if (!Buf)
232 exitWithErrorCode(Buf.getError(), InputFilenamesFile);
233
234 StringRef Data = Buf.get()->getBuffer();
229 return {};
230
231 auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile);
232 if (!BufOrError)
233 exitWithErrorCode(BufOrError.getError(), InputFilenamesFile);
234
235 std::unique_ptr Buffer = std::move(*BufOrError);
236 StringRef Data = Buffer->getBuffer();
237
235238 SmallVector Entries;
236239 Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
237240 for (const StringRef &FileWeightEntry : Entries) {
245248 else
246249 WFV.emplace_back(parseWeightedFile(SanitizedEntry));
247250 }
251
252 return Buffer;
248253 }
249254
250255 static int merge_main(int argc, const char *argv[]) {
287292 WeightedInputs.push_back(WeightedFile(Filename, 1));
288293 for (StringRef WeightedFilename : WeightedInputFilenames)
289294 WeightedInputs.push_back(parseWeightedFile(WeightedFilename));
290 parseInputFilenamesFile(InputFilenamesFile, WeightedInputs);
295 auto Buf = parseInputFilenamesFile(InputFilenamesFile, WeightedInputs);
291296
292297 if (WeightedInputs.empty())
293298 exitWithError("No input files specified. See " +