diff --git a/clang/lib/Tooling/JSONCompilationDatabase.cpp b/clang/lib/Tooling/JSONCompilationDatabase.cpp --- a/clang/lib/Tooling/JSONCompilationDatabase.cpp +++ b/clang/lib/Tooling/JSONCompilationDatabase.cpp @@ -217,7 +217,7 @@ std::string &ErrorMessage, JSONCommandLineSyntax Syntax) { std::unique_ptr DatabaseBuffer( - llvm::MemoryBuffer::getMemBuffer(DatabaseString)); + llvm::MemoryBuffer::getMemBufferCopy(DatabaseString)); std::unique_ptr Database( new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax)); if (!Database->parse(ErrorMessage)) diff --git a/clang/unittests/Tooling/CompilationDatabaseTest.cpp b/clang/unittests/Tooling/CompilationDatabaseTest.cpp --- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/clang/unittests/Tooling/CompilationDatabaseTest.cpp @@ -172,13 +172,15 @@ } static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName, - StringRef JSONDatabase, + std::string JSONDatabase, std::string &ErrorMessage) { std::unique_ptr Database( JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage, JSONCommandLineSyntax::Gnu)); if (!Database) return CompileCommand(); + // Overwrite the string to verify we're not reading from it later. + JSONDatabase.assign(JSONDatabase.size(), '*'); std::vector Commands = Database->getCompileCommands(FileName); EXPECT_LE(Commands.size(), 1u); if (Commands.empty())