ELF: Fix use-after-free problem.
authorRui Ueyama <ruiu@google.com>
Tue, 15 Mar 2016 18:20:50 +0000 (18:20 +0000)
committerRui Ueyama <ruiu@google.com>
Tue, 15 Mar 2016 18:20:50 +0000 (18:20 +0000)
Fixes pr26908. This patch is based on Filipe Cabecinhas'
patch (http://reviews.llvm.org/D18167)

http://reviews.llvm.org/D18169

llvm-svn: 263569

lld/ELF/Driver.cpp
lld/ELF/Driver.h
lld/ELF/DriverUtils.cpp

index de873bb..b7cf7a3 100644 (file)
@@ -170,7 +170,8 @@ static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
 }
 
 void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
-  opt::InputArgList Args = parseArgs(&Alloc, ArgsArr.slice(1));
+  ELFOptTable Parser;
+  opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
   if (Args.hasArg(OPT_help)) {
     printHelp(ArgsArr[0]);
     return;
index 33b5580..0437ebf 100644 (file)
@@ -39,8 +39,14 @@ private:
 };
 
 // Parses command line options.
-llvm::opt::InputArgList parseArgs(llvm::BumpPtrAllocator *A,
-                                  ArrayRef<const char *> Args);
+class ELFOptTable : public llvm::opt::OptTable {
+public:
+  ELFOptTable();
+  llvm::opt::InputArgList parse(ArrayRef<const char *> Argv);
+
+private:
+  llvm::BumpPtrAllocator Alloc;
+};
 
 // Create enum with OPT_xxx values for each option in Options.td
 enum {
index 851166e..f40cfdd 100644 (file)
@@ -46,26 +46,21 @@ static const opt::OptTable::Info infoTable[] = {
 #undef OPTION
 };
 
-class ELFOptTable : public opt::OptTable {
-public:
-  ELFOptTable() : OptTable(infoTable) {}
-};
+ELFOptTable::ELFOptTable() : OptTable(infoTable) {}
 
 // Parses a given list of options.
-opt::InputArgList elf::parseArgs(llvm::BumpPtrAllocator *A,
-                                 ArrayRef<const char *> Argv) {
+opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) {
   // Make InputArgList from string vectors.
-  ELFOptTable Table;
   unsigned MissingIndex;
   unsigned MissingCount;
 
   // Expand response files. '@<filename>' is replaced by the file's contents.
   SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
-  StringSaver Saver(*A);
+  StringSaver Saver(Alloc);
   llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, Vec);
 
   // Parse options and then do error checking.
-  opt::InputArgList Args = Table.ParseArgs(Vec, MissingIndex, MissingCount);
+  opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
   if (MissingCount)
     error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) +
           "\", expected " + Twine(MissingCount) +