[UI] Fix UB (moved mem) in file picker

- References to vector data become UB after vector size changes.
- Add one extra level of indirection to pin the wide string memory
  location regardless of vector memory
This commit is contained in:
Joel Linn 2022-11-20 19:48:13 +01:00 committed by Rick Gibbed
parent 7877331d8a
commit f452d6a007
1 changed files with 7 additions and 5 deletions

View File

@ -148,14 +148,16 @@ bool Win32FilePicker::Show(Window* parent_window) {
if (type() == Type::kFile) { if (type() == Type::kFile) {
// Set the file types to display only. Notice that this is a 1-based array. // Set the file types to display only. Notice that this is a 1-based array.
std::vector<std::pair<std::u16string, std::u16string>> file_pairs; using u16sPair = std::pair<std::u16string, std::u16string>;
std::vector<std::unique_ptr<u16sPair>> file_pairs;
std::vector<COMDLG_FILTERSPEC> file_types; std::vector<COMDLG_FILTERSPEC> file_types;
for (const auto& extension : this->extensions()) { for (const auto& extension : this->extensions()) {
const auto& file_pair = const auto& file_pair =
file_pairs.emplace_back(std::move(xe::to_utf16(extension.first)), file_pairs.emplace_back(std::make_unique<u16sPair>(
std::move(xe::to_utf16(extension.second))); xe::to_utf16(extension.first), xe::to_utf16(extension.second)));
file_types.push_back({(LPCWSTR)file_pair.first.c_str(), file_types.push_back(
(LPCWSTR)file_pair.second.c_str()}); {reinterpret_cast<LPCWSTR>(file_pair->first.c_str()),
reinterpret_cast<LPCWSTR>(file_pair->second.c_str())});
} }
hr = file_dialog->SetFileTypes(static_cast<UINT>(file_types.size()), hr = file_dialog->SetFileTypes(static_cast<UINT>(file_types.size()),
file_types.data()); file_types.data());