Author: Alexandre Ganea
Date: 2021-01-07T15:15:13-05:00
New Revision: 3854b81b0fd23adc9bab91bf68918d102dc31f51

URL: 
https://github.com/llvm/llvm-project/commit/3854b81b0fd23adc9bab91bf68918d102dc31f51
DIFF: 
https://github.com/llvm/llvm-project/commit/3854b81b0fd23adc9bab91bf68918d102dc31f51.diff

LOG: [Clang][Driver] Fix read-after-free when using /clang:

Fixes PR42501.

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

Added: 
    

Modified: 
    clang/lib/Driver/Driver.cpp
    clang/test/Driver/cl-options.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 5c3ce478053a..418e1d3e8ec9 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1009,13 +1009,15 @@ Compilation *Driver::BuildCompilation(ArrayRef<const 
char *> ArgList) {
   // objects than Args. This copies an Arg from one of those other 
InputArgLists
   // to the ownership of Args.
   auto appendOneArg = [&Args](const Arg *Opt, const Arg *BaseArg) {
-      unsigned Index = Args.MakeIndex(Opt->getSpelling());
-      Arg *Copy = new llvm::opt::Arg(Opt->getOption(), Opt->getSpelling(),
-                                     Index, BaseArg);
-      Copy->getValues() = Opt->getValues();
-      if (Opt->isClaimed())
-        Copy->claim();
-      Args.append(Copy);
+    unsigned Index = Args.MakeIndex(Opt->getSpelling());
+    Arg *Copy = new llvm::opt::Arg(Opt->getOption(), Args.getArgString(Index),
+                                   Index, BaseArg);
+    Copy->getValues() = Opt->getValues();
+    if (Opt->isClaimed())
+      Copy->claim();
+    Copy->setOwnsValues(Opt->getOwnsValues());
+    Opt->setOwnsValues(false);
+    Args.append(Copy);
   };
 
   if (HasConfigFile)

diff  --git a/clang/test/Driver/cl-options.c b/clang/test/Driver/cl-options.c
index db70fca5222c..4b6d71ed7b6d 100644
--- a/clang/test/Driver/cl-options.c
+++ b/clang/test/Driver/cl-options.c
@@ -686,6 +686,11 @@
 // CLANG-NOT: "--dependent-lib=libcmt"
 // CLANG-NOT: "-vectorize-slp"
 
+// Cover PR42501: clang-cl /clang: pass-through causes read-after-free with 
aliased options.
+// RUN: %clang_cl /clang:-save-temps /clang:-Wl,test1,test2 -### -- %s 2>&1 | 
FileCheck -check-prefix=SAVETEMPS %s
+// SAVETEMPS: "-save-temps=cwd"
+// SAVETEMPS: "test1" "test2"
+
 // Validate that the default triple is used when run an empty tools dir is 
specified
 // RUN: %clang_cl -vctoolsdir "" -### -- %s 2>&1 | FileCheck %s --check-prefix 
VCTOOLSDIR
 // VCTOOLSDIR: "-triple" "{{[a-zA-Z0-9_-]*}}-pc-windows-msvc19.11.0"


        
_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to