From f452d6a007f7b32fcbe94bff779381467b60d142 Mon Sep 17 00:00:00 2001 From: Joel Linn Date: Sun, 20 Nov 2022 19:48:13 +0100 Subject: [PATCH] [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 --- src/xenia/ui/file_picker_win.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/xenia/ui/file_picker_win.cc b/src/xenia/ui/file_picker_win.cc index 5cb467ca1..ec64ff595 100644 --- a/src/xenia/ui/file_picker_win.cc +++ b/src/xenia/ui/file_picker_win.cc @@ -148,14 +148,16 @@ bool Win32FilePicker::Show(Window* parent_window) { if (type() == Type::kFile) { // Set the file types to display only. Notice that this is a 1-based array. - std::vector> file_pairs; + using u16sPair = std::pair; + std::vector> file_pairs; std::vector file_types; for (const auto& extension : this->extensions()) { const auto& file_pair = - file_pairs.emplace_back(std::move(xe::to_utf16(extension.first)), - std::move(xe::to_utf16(extension.second))); - file_types.push_back({(LPCWSTR)file_pair.first.c_str(), - (LPCWSTR)file_pair.second.c_str()}); + file_pairs.emplace_back(std::make_unique( + xe::to_utf16(extension.first), xe::to_utf16(extension.second))); + file_types.push_back( + {reinterpret_cast(file_pair->first.c_str()), + reinterpret_cast(file_pair->second.c_str())}); } hr = file_dialog->SetFileTypes(static_cast(file_types.size()), file_types.data());