[ConfigFiles] Update argument strings when merging argrument lists
authorSerge Pavlov <sepavloff@gmail.com>
Thu, 26 Apr 2018 06:28:47 +0000 (06:28 +0000)
committerSerge Pavlov <sepavloff@gmail.com>
Thu, 26 Apr 2018 06:28:47 +0000 (06:28 +0000)
Implementation of `InputArgList` assumes its field `ArgStrings` contains
strings for each argument exactly in the same order. This condition was
broken when arguments from config file and from invocation were merged.

This change fixes https://bugs.llvm.org/show_bug.cgi?id=37196 (Clang
config files can crash argument handling).

llvm-svn: 330926

clang/lib/Driver/Driver.cpp
clang/test/Driver/Inputs/empty.cfg [new file with mode: 0644]
clang/test/Driver/config-file4.c [new file with mode: 0644]

index 932a54f..c119f33 100644 (file)
@@ -876,11 +876,14 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
                                               : std::move(*CLOptions));
   if (HasConfigFile)
     for (auto *Opt : *CLOptions) {
+      if (Opt->getOption().matches(options::OPT_config))
+        continue;
+      unsigned Index = Args.MakeIndex(Opt->getSpelling());
       const Arg *BaseArg = &Opt->getBaseArg();
       if (BaseArg == Opt)
         BaseArg = nullptr;
       Arg *Copy = new llvm::opt::Arg(Opt->getOption(), Opt->getSpelling(),
-                                     Args.size(), BaseArg);
+                                     Index, BaseArg);
       Copy->getValues() = Opt->getValues();
       if (Opt->isClaimed())
         Copy->claim();
diff --git a/clang/test/Driver/Inputs/empty.cfg b/clang/test/Driver/Inputs/empty.cfg
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/clang/test/Driver/config-file4.c b/clang/test/Driver/config-file4.c
new file mode 100644 (file)
index 0000000..f61e317
--- /dev/null
@@ -0,0 +1,2 @@
+// RUN: %clang --config %S/Inputs/empty.cfg -Wall -Wextra -Wformat -Wstrict-aliasing -Wshadow -Wpacked -Winline -Wimplicit-function-declaration -c %s -O2 -o /dev/null -v 2>&1 | FileCheck %s -check-prefix PR37196
+// PR37196: Configuration file: {{.*}}/empty.cfg