Index: clang/lib/Tooling/CompilationDatabase.cpp =================================================================== --- clang/lib/Tooling/CompilationDatabase.cpp +++ clang/lib/Tooling/CompilationDatabase.cpp @@ -345,7 +345,7 @@ FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) { ErrorMsg.clear(); llvm::ErrorOr> File = - llvm::MemoryBuffer::getFile(Path); + llvm::MemoryBuffer::getFile(Path, -1, true); if (std::error_code Result = File.getError()) { ErrorMsg = "Error while opening fixed database: " + Result.message(); return nullptr; Index: clang/tools/clang-refactor/ClangRefactor.cpp =================================================================== --- clang/tools/clang-refactor/ClangRefactor.cpp +++ clang/tools/clang-refactor/ClangRefactor.cpp @@ -484,7 +484,7 @@ Spec.Cleanup = false; for (const auto &File : Files) { llvm::ErrorOr> BufferErr = - llvm::MemoryBuffer::getFile(File); + llvm::MemoryBuffer::getFile(File, -1, true); if (!BufferErr) { llvm::errs() << "error: failed to open " << File << " for rewriting\n"; return true; Index: clang/tools/clang-refactor/TestSupport.cpp =================================================================== --- clang/tools/clang-refactor/TestSupport.cpp +++ clang/tools/clang-refactor/TestSupport.cpp @@ -91,7 +91,7 @@ Spec.Cleanup = false; for (const auto &File : Files) { llvm::ErrorOr> BufferErr = - llvm::MemoryBuffer::getFile(File); + llvm::MemoryBuffer::getFile(File, -1, true); if (!BufferErr) { llvm::errs() << "failed to open" << File << "\n"; return true; @@ -161,7 +161,7 @@ std::pair getLineColumn(StringRef Filename, unsigned Offset) { ErrorOr> ErrOrFile = - MemoryBuffer::getFile(Filename); + MemoryBuffer::getFile(Filename, -1, true); if (!ErrOrFile) return {0, 0}; StringRef Source = ErrOrFile.get()->getBuffer(); @@ -294,7 +294,7 @@ Optional findTestSelectionRanges(StringRef Filename) { ErrorOr> ErrOrFile = - MemoryBuffer::getFile(Filename); + MemoryBuffer::getFile(Filename, -1, true); if (!ErrOrFile) { llvm::errs() << "error: -selection=test:" << Filename << " : could not open the given file"; Index: lld/COFF/Driver.cpp =================================================================== --- lld/COFF/Driver.cpp +++ lld/COFF/Driver.cpp @@ -105,9 +105,7 @@ auto Strategy = std::launch::deferred; #endif return std::async(Strategy, [=]() { - auto MBOrErr = MemoryBuffer::getFile(Path, - /*FileSize*/ -1, - /*RequiresNullTerminator*/ false); + auto MBOrErr = MemoryBuffer::getFile(Path); if (!MBOrErr) return MBErrPair{nullptr, MBOrErr.getError()}; return MBErrPair{std::move(*MBOrErr), std::error_code()}; @@ -565,8 +563,7 @@ // If the import library already exists, replace it only if the contents // have changed. - ErrorOr> OldBuf = MemoryBuffer::getFile( - Path, /*FileSize*/ -1, /*RequiresNullTerminator*/ false); + ErrorOr> OldBuf = MemoryBuffer::getFile(Path); if (!OldBuf) { HandleError(writeImportLibrary(LibName, Path, Exports, Config->Machine, false, Config->MinGW)); @@ -585,8 +582,7 @@ return; } - std::unique_ptr NewBuf = check(MemoryBuffer::getFile( - TmpName, /*FileSize*/ -1, /*RequiresNullTerminator*/ false)); + std::unique_ptr NewBuf = check(MemoryBuffer::getFile(TmpName)); if ((*OldBuf)->getBuffer() != NewBuf->getBuffer()) { OldBuf->reset(); HandleError(errorCodeToError(sys::fs::rename(TmpName, Path))); Index: llvm/include/llvm/Support/MemoryBuffer.h =================================================================== --- llvm/include/llvm/Support/MemoryBuffer.h +++ llvm/include/llvm/Support/MemoryBuffer.h @@ -80,7 +80,7 @@ /// while the user is editing/updating the file or if the file is on an NFS. static ErrorOr> getFile(const Twine &Filename, int64_t FileSize = -1, - bool RequiresNullTerminator = true, bool IsVolatile = false); + bool RequiresNullTerminator = false, bool IsVolatile = false); /// Read all of the specified file into a MemoryBuffer as a stream /// (i.e. until EOF reached). This is useful for special files that @@ -103,16 +103,16 @@ /// while the user is editing/updating the file or if the file is on an NFS. static ErrorOr> getOpenFile(int FD, const Twine &Filename, uint64_t FileSize, - bool RequiresNullTerminator = true, bool IsVolatile = false); + bool RequiresNullTerminator = false, bool IsVolatile = false); /// Open the specified memory range as a MemoryBuffer. Note that InputData /// must be null terminated if RequiresNullTerminator is true. static std::unique_ptr getMemBuffer(StringRef InputData, StringRef BufferName = "", - bool RequiresNullTerminator = true); + bool RequiresNullTerminator = false); static std::unique_ptr - getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true); + getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = false); /// Open the specified memory range as a MemoryBuffer, copying the contents /// and taking ownership of it. InputData does not have to be null terminated. Index: llvm/lib/AsmParser/Parser.cpp =================================================================== --- llvm/lib/AsmParser/Parser.cpp +++ llvm/lib/AsmParser/Parser.cpp @@ -52,7 +52,7 @@ LLVMContext &Context, SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) { ErrorOr> FileOrErr = - MemoryBuffer::getFileOrSTDIN(Filename); + MemoryBuffer::getFileOrSTDIN(Filename, -1, true); if (std::error_code EC = FileOrErr.getError()) { Err = SMDiagnostic(Filename, SourceMgr::DK_Error, "Could not open input file: " + EC.message()); Index: llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp =================================================================== --- llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp +++ llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp @@ -41,7 +41,7 @@ return; ErrorOr> BufOrErr = - MemoryBuffer::getFile(FileName); + MemoryBuffer::getFile(FileName, -1, true); if (!BufOrErr) return; Index: llvm/lib/Support/SourceMgr.cpp =================================================================== --- llvm/lib/Support/SourceMgr.cpp +++ llvm/lib/Support/SourceMgr.cpp @@ -42,14 +42,14 @@ std::string &IncludedFile) { IncludedFile = Filename; ErrorOr> NewBufOrErr = - MemoryBuffer::getFile(IncludedFile); + MemoryBuffer::getFile(IncludedFile, -1, /*RequiresNullTerminator=*/true); // If the file didn't exist directly, see if it's in an include path. for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr; ++i) { IncludedFile = IncludeDirectories[i] + sys::path::get_separator().data() + Filename; - NewBufOrErr = MemoryBuffer::getFile(IncludedFile); + NewBufOrErr = MemoryBuffer::getFile(IncludedFile, -1, true); } if (!NewBufOrErr) Index: llvm/lib/TableGen/Main.cpp =================================================================== --- llvm/lib/TableGen/Main.cpp +++ llvm/lib/TableGen/Main.cpp @@ -79,7 +79,7 @@ // Parse the input file. ErrorOr> FileOrErr = - MemoryBuffer::getFileOrSTDIN(InputFilename); + MemoryBuffer::getFileOrSTDIN(InputFilename, -1, true); if (std::error_code EC = FileOrErr.getError()) return reportError(argv0, "Could not open input file '" + InputFilename + "': " + EC.message() + "\n"); Index: llvm/tools/lli/lli.cpp =================================================================== --- llvm/tools/lli/lli.cpp +++ llvm/tools/lli/lli.cpp @@ -220,7 +220,7 @@ return nullptr; // Load the object from the cache filename ErrorOr> IRObjectBuffer = - MemoryBuffer::getFile(CacheName, -1, false); + MemoryBuffer::getFile(CacheName); // If the file isn't there, that's OK. if (!IRObjectBuffer) return nullptr; Index: llvm/tools/llvm-ar/llvm-ar.cpp =================================================================== --- llvm/tools/llvm-ar/llvm-ar.cpp +++ llvm/tools/llvm-ar/llvm-ar.cpp @@ -749,7 +749,7 @@ std::vector *NewMembers) { // Create or open the archive object. ErrorOr> Buf = - MemoryBuffer::getFile(ArchiveName, -1, false); + MemoryBuffer::getFile(ArchiveName); std::error_code EC = Buf.getError(); if (EC && EC != errc::no_such_file_or_directory) fail("error opening '" + ArchiveName + "': " + EC.message() + "!"); @@ -808,7 +808,7 @@ switch (Command) { case MRICommand::AddLib: { - auto BufOrErr = MemoryBuffer::getFile(Rest, -1, false); + auto BufOrErr = MemoryBuffer::getFile(Rest); failIfError(BufOrErr.getError(), "Could not open library"); ArchiveBuffers.push_back(std::move(*BufOrErr)); auto LibOrErr = Index: llvm/tools/llvm-cov/CodeCoverage.cpp =================================================================== --- llvm/tools/llvm-cov/CodeCoverage.cpp +++ llvm/tools/llvm-cov/CodeCoverage.cpp @@ -238,7 +238,7 @@ for (const auto &Files : LoadedSourceFiles) if (sys::fs::equivalent(SourceFile, Files.first)) return *Files.second; - auto Buffer = MemoryBuffer::getFile(SourceFile); + auto Buffer = MemoryBuffer::getFile(SourceFile, -1, true); if (auto EC = Buffer.getError()) { error(EC.message(), SourceFile); return EC; Index: llvm/tools/llvm-cov/gcov.cpp =================================================================== --- llvm/tools/llvm-cov/gcov.cpp +++ llvm/tools/llvm-cov/gcov.cpp @@ -45,7 +45,7 @@ GCOVFile GF; ErrorOr> GCNO_Buff = - MemoryBuffer::getFileOrSTDIN(GCNO); + MemoryBuffer::getFileOrSTDIN(GCNO, -1, true); if (std::error_code EC = GCNO_Buff.getError()) { errs() << GCNO << ": " << EC.message() << "\n"; return; @@ -57,7 +57,7 @@ } ErrorOr> GCDA_Buff = - MemoryBuffer::getFileOrSTDIN(GCDA); + MemoryBuffer::getFileOrSTDIN(GCDA, -1, true); if (std::error_code EC = GCDA_Buff.getError()) { if (EC != errc::no_such_file_or_directory) { errs() << GCDA << ": " << EC.message() << "\n"; Index: llvm/tools/llvm-opt-report/OptReport.cpp =================================================================== --- llvm/tools/llvm-opt-report/OptReport.cpp +++ llvm/tools/llvm-opt-report/OptReport.cpp @@ -310,7 +310,7 @@ const auto &FileInfo = FI.second; ErrorOr> Buf = - MemoryBuffer::getFile(FileName); + MemoryBuffer::getFile(FileName, -1, true); if (std::error_code EC = Buf.getError()) { WithColor::error() << "Can't open file " << FileName << ": " << EC.message() << "\n"; Index: llvm/tools/llvm-pdbutil/InputFile.cpp =================================================================== --- llvm/tools/llvm-pdbutil/InputFile.cpp +++ llvm/tools/llvm-pdbutil/InputFile.cpp @@ -280,7 +280,7 @@ formatv("File {0} is not a supported file type", Path), inconvertibleErrorCode()); - auto Result = MemoryBuffer::getFile(Path, -1LL, false); + auto Result = MemoryBuffer::getFile(Path); if (!Result) return make_error( formatv("File {0} could not be opened", Path), Result.getError()); Index: llvm/tools/llvm-rc/ResourceFileWriter.cpp =================================================================== --- llvm/tools/llvm-rc/ResourceFileWriter.cpp +++ llvm/tools/llvm-rc/ResourceFileWriter.cpp @@ -1418,7 +1418,7 @@ Path.assign(Cwd.begin(), Cwd.end()); sys::path::append(Path, File); if (sys::fs::exists(Path)) - return errorOrToExpected(MemoryBuffer::getFile(Path, -1, false)); + return errorOrToExpected(MemoryBuffer::getFile(Path)); // 2. The directory of the input resource file, if it is different from the // current @@ -1427,19 +1427,19 @@ Path.assign(InputFileDir.begin(), InputFileDir.end()); sys::path::append(Path, File); if (sys::fs::exists(Path)) - return errorOrToExpected(MemoryBuffer::getFile(Path, -1, false)); + return errorOrToExpected(MemoryBuffer::getFile(Path)); // 3. All of the include directories specified on the command line. for (StringRef ForceInclude : Params.Include) { Path.assign(ForceInclude.begin(), ForceInclude.end()); sys::path::append(Path, File); if (sys::fs::exists(Path)) - return errorOrToExpected(MemoryBuffer::getFile(Path, -1, false)); + return errorOrToExpected(MemoryBuffer::getFile(Path)); } if (auto Result = llvm::sys::Process::FindInEnvPath("INCLUDE", File, Params.NoInclude)) - return errorOrToExpected(MemoryBuffer::getFile(*Result, -1, false)); + return errorOrToExpected(MemoryBuffer::getFile(*Result)); return make_error("error : file not found : " + Twine(File), inconvertibleErrorCode()); Index: llvm/tools/sanstats/sanstats.cpp =================================================================== --- llvm/tools/sanstats/sanstats.cpp +++ llvm/tools/sanstats/sanstats.cpp @@ -117,7 +117,7 @@ "Sanitizer Statistics Processing Tool"); ErrorOr> MBOrErr = - MemoryBuffer::getFile(ClInputFile, -1, false); + MemoryBuffer::getFile(ClInputFile); if (!MBOrErr) { errs() << argv[0] << ": " << ClInputFile << ": " << MBOrErr.getError().message() << '\n'; Index: llvm/unittests/AsmParser/AsmParserTest.cpp =================================================================== --- llvm/unittests/AsmParser/AsmParserTest.cpp +++ llvm/unittests/AsmParser/AsmParserTest.cpp @@ -30,22 +30,6 @@ EXPECT_TRUE(Error.getMessage().empty()); } -#ifdef GTEST_HAS_DEATH_TEST -#ifndef NDEBUG - -TEST(AsmParserTest, NonNullTerminatedInput) { - LLVMContext Ctx; - StringRef Source = "; Empty module \n\1\2"; - SMDiagnostic Error; - std::unique_ptr Mod; - EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2), - Error, Ctx), - "Buffer is not null terminated!"); -} - -#endif -#endif - TEST(AsmParserTest, SlotMappingTest) { LLVMContext Ctx; StringRef Source = "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}";