diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc --- a/llvm/lib/Support/Windows/Path.inc +++ b/llvm/lib/Support/Windows/Path.inc @@ -132,7 +132,8 @@ std::string getMainExecutable(const char *argv0, void *MainExecAddr) { SmallVector PathName; - DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity()); + PathName.resize_for_overwrite(PathName.capacity()); + DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.size()); // A zero return value indicates a failure other than insufficient space. if (Size == 0) @@ -145,7 +146,7 @@ // On success, GetModuleFileNameW returns the number of characters written to // the buffer not including the NULL terminator. - PathName.set_size(Size); + PathName.truncate(Size); // Convert the result from UTF-16 to UTF-8. SmallVector PathNameUTF8; @@ -201,8 +202,8 @@ DWORD len = MAX_PATH; do { - cur_path.reserve(len); - len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data()); + cur_path.resize_for_overwrite(len); + len = ::GetCurrentDirectoryW(cur_path.size(), cur_path.data()); // A zero return value indicates a failure other than insufficient space. if (len == 0) @@ -210,11 +211,11 @@ // If there's insufficient space, the len returned is larger than the len // given. - } while (len > cur_path.capacity()); + } while (len > cur_path.size()); // On success, GetCurrentDirectoryW returns the number of characters not // including the null-terminator. - cur_path.set_size(len); + cur_path.truncate(len); if (std::error_code EC = UTF16ToUTF8(cur_path.begin(), cur_path.size(), result)) @@ -328,7 +329,7 @@ // the null terminator, it will leave the output unterminated. Push a null // terminator onto the end to ensure that this never happens. VolumePath.push_back(L'\0'); - VolumePath.set_size(wcslen(VolumePath.data())); + VolumePath.truncate(wcslen(VolumePath.data())); const wchar_t *P = VolumePath.data(); UINT Type = ::GetDriveTypeW(P); @@ -364,18 +365,19 @@ static std::error_code realPathFromHandle(HANDLE H, SmallVectorImpl &Buffer) { + Buffer.resize_for_overwrite(Buffer.capacity()); DWORD CountChars = ::GetFinalPathNameByHandleW( H, Buffer.begin(), Buffer.capacity(), FILE_NAME_NORMALIZED); if (CountChars && CountChars >= Buffer.capacity()) { // The buffer wasn't big enough, try again. In this case the return value // *does* indicate the size of the null terminator. - Buffer.reserve(CountChars); + Buffer.resize_for_overwrite(CountChars); CountChars = ::GetFinalPathNameByHandleW( - H, Buffer.begin(), Buffer.capacity(), FILE_NAME_NORMALIZED); + H, Buffer.begin(), Buffer.size(), FILE_NAME_NORMALIZED); } + Buffer.truncate(CountChars); if (CountChars == 0) return mapWindowsError(GetLastError()); - Buffer.set_size(CountChars); return std::error_code(); } @@ -1450,14 +1452,14 @@ SmallVector Buf; size_t Size = 1024; do { - Buf.reserve(Size); - Size = GetEnvironmentVariableW(Var, Buf.data(), Buf.capacity()); + Buf.resize_for_overwrite(Size); + Size = GetEnvironmentVariableW(Var, Buf.data(), Buf.size()); if (Size == 0) return false; // Try again with larger buffer. - } while (Size > Buf.capacity()); - Buf.set_size(Size); + } while (Size > Buf.size()); + Buf.truncate(Size); return !windows::UTF16ToUTF8(Buf.data(), Size, Res); } @@ -1506,7 +1508,7 @@ } utf16.reserve(len + 1); - utf16.set_size(len); + utf16.resize_for_overwrite(len); len = ::MultiByteToWideChar(codepage, MB_ERR_INVALID_CHARS, original.begin(), original.size(), utf16.begin(), utf16.size()); @@ -1546,8 +1548,8 @@ return mapWindowsError(::GetLastError()); } - converted.reserve(len); - converted.set_size(len); + converted.reserve(len + 1); + converted.resize_for_overwrite(len); // Now do the actual conversion. len = ::WideCharToMultiByte(codepage, 0, utf16, utf16_len, converted.data(), diff --git a/llvm/lib/Support/Windows/Process.inc b/llvm/lib/Support/Windows/Process.inc --- a/llvm/lib/Support/Windows/Process.inc +++ b/llvm/lib/Support/Windows/Process.inc @@ -129,16 +129,16 @@ SmallVector Buf; size_t Size = MAX_PATH; do { - Buf.reserve(Size); + Buf.resize_for_overwrite(Size); SetLastError(NO_ERROR); Size = - GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity()); + GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.size()); if (Size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) return None; // Try again with larger buffer. - } while (Size > Buf.capacity()); - Buf.set_size(Size); + } while (Size > Buf.size()); + Buf.truncate(Size); // Convert the result from UTF-16 to UTF-8. SmallVector Res; diff --git a/llvm/lib/Support/Windows/Program.inc b/llvm/lib/Support/Windows/Program.inc --- a/llvm/lib/Support/Windows/Program.inc +++ b/llvm/lib/Support/Windows/Program.inc @@ -72,7 +72,7 @@ SmallVector U16Result; DWORD Len = MAX_PATH; do { - U16Result.reserve(Len); + U16Result.resize_for_overwrite(Len); // Lets attach the extension manually. That is needed for files // with a point in name like aaa.bbb. SearchPathW will not add extension // from its argument to such files because it thinks they already had one. @@ -82,13 +82,13 @@ return EC; Len = ::SearchPathW(Path, c_str(U16NameExt), nullptr, - U16Result.capacity(), U16Result.data(), nullptr); - } while (Len > U16Result.capacity()); + U16Result.size(), U16Result.data(), nullptr); + } while (Len > U16Result.size()); if (Len == 0) continue; - U16Result.set_size(Len); + U16Result.truncate(Len); if (std::error_code EC = windows::UTF16ToUTF8(U16Result.data(), U16Result.size(), U8Result))