marcan created this revision.
marcan added reviewers: sepavloff, echristo.
Herald added a subscriber: cfe-commits.

Doing an `.insert()` can potentially invalidate iterators by reallocating the 
vector's storage. When all the stars align just right, this causes segfaults or 
glibc aborts. Fix this by recomputing the position before each insert.

Gentoo Linux bug (crashes while building Chromium): 
https://bugs.gentoo.org/650082


Repository:
  rC Clang

https://reviews.llvm.org/D44607

Files:
  tools/driver/driver.cpp


Index: tools/driver/driver.cpp
===================================================================
--- tools/driver/driver.cpp
+++ tools/driver/driver.cpp
@@ -209,20 +209,24 @@
 static void insertTargetAndModeArgs(const ParsedClangName &NameParts,
                                     SmallVectorImpl<const char *> &ArgVector,
                                     std::set<std::string> &SavedStrings) {
-  // Put target and mode arguments at the start of argument list so that
-  // arguments specified in command line could override them. Avoid putting
-  // them at index 0, as an option like '-cc1' must remain the first.
-  auto InsertionPoint = ArgVector.begin();
-  if (InsertionPoint != ArgVector.end())
-    ++InsertionPoint;
-
   if (NameParts.DriverMode) {
+    // Put target and mode arguments at the start of argument list so that
+    // arguments specified in command line could override them. Avoid putting
+    // them at index 0, as an option like '-cc1' must remain the first.
+    auto InsertionPoint = ArgVector.begin();
+    if (InsertionPoint != ArgVector.end())
+        ++InsertionPoint;
+
     // Add the mode flag to the arguments.
     ArgVector.insert(InsertionPoint,
                      GetStableCStr(SavedStrings, NameParts.DriverMode));
   }
 
   if (NameParts.TargetIsValid) {
+    auto InsertionPoint = ArgVector.begin();
+    if (InsertionPoint != ArgVector.end())
+        ++InsertionPoint;
+
     const char *arr[] = {"-target", GetStableCStr(SavedStrings,
                                                   NameParts.TargetPrefix)};
     ArgVector.insert(InsertionPoint, std::begin(arr), std::end(arr));


Index: tools/driver/driver.cpp
===================================================================
--- tools/driver/driver.cpp
+++ tools/driver/driver.cpp
@@ -209,20 +209,24 @@
 static void insertTargetAndModeArgs(const ParsedClangName &NameParts,
                                     SmallVectorImpl<const char *> &ArgVector,
                                     std::set<std::string> &SavedStrings) {
-  // Put target and mode arguments at the start of argument list so that
-  // arguments specified in command line could override them. Avoid putting
-  // them at index 0, as an option like '-cc1' must remain the first.
-  auto InsertionPoint = ArgVector.begin();
-  if (InsertionPoint != ArgVector.end())
-    ++InsertionPoint;
-
   if (NameParts.DriverMode) {
+    // Put target and mode arguments at the start of argument list so that
+    // arguments specified in command line could override them. Avoid putting
+    // them at index 0, as an option like '-cc1' must remain the first.
+    auto InsertionPoint = ArgVector.begin();
+    if (InsertionPoint != ArgVector.end())
+        ++InsertionPoint;
+
     // Add the mode flag to the arguments.
     ArgVector.insert(InsertionPoint,
                      GetStableCStr(SavedStrings, NameParts.DriverMode));
   }
 
   if (NameParts.TargetIsValid) {
+    auto InsertionPoint = ArgVector.begin();
+    if (InsertionPoint != ArgVector.end())
+        ++InsertionPoint;
+
     const char *arr[] = {"-target", GetStableCStr(SavedStrings,
                                                   NameParts.TargetPrefix)};
     ArgVector.insert(InsertionPoint, std::begin(arr), std::end(arr));
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to