[PATCH] D44607: Recompute invalidated iterator in insertTargetAndModeArgs

Hector Martin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 18 06:42:45 PDT 2018


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));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44607.138847.patch
Type: text/x-patch
Size: 1643 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180318/b73f10c2/attachment-0001.bin>


More information about the cfe-commits mailing list