[Driver] Avoid invalidated iterator in insertTargetAndModeArgs

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.

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

Patch by Hector Martin!

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

llvm-svn: 327863
This commit is contained in:
Serge Pavlov 2018-03-19 16:13:43 +00:00
parent 4b54e5fc38
commit 826e833121
1 changed files with 5 additions and 4 deletions

View File

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