[clang][Tooling] Fix potential UB in ExpandResponseFilesCompilationDatabase
authorSam McCall <sam.mccall@gmail.com>
Mon, 9 Dec 2019 11:04:05 +0000 (12:04 +0100)
committerSam McCall <sam.mccall@gmail.com>
Mon, 9 Dec 2019 11:24:23 +0000 (12:24 +0100)
Summary:
`vector::assign` will cause UB at here.

fixes: https://github.com/clangd/clangd/issues/223

Reviewers: kadircet, sammccall, hokein

Reviewed By: sammccall

Subscribers: merge_guards_bot, ilya-biryukov, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D71172

clang/lib/Tooling/ExpandResponseFilesCompilationDatabase.cpp

index 84936ba..9929831 100644 (file)
@@ -61,7 +61,9 @@ private:
       llvm::StringSaver Saver(Alloc);
       llvm::cl::ExpandResponseFiles(Saver, Tokenizer, Argv, false, false, *FS,
                                     llvm::StringRef(Cmd.Directory));
-      Cmd.CommandLine.assign(Argv.begin(), Argv.end());
+      // Don't assign directly, Argv aliases CommandLine.
+      std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
+      Cmd.CommandLine = std::move(ExpandedArgv);
     }
     return Cmds;
   }