From fe84448bf5a33572acee2014c4526bb45339aed4 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 8 Feb 2025 18:52:13 +0100 Subject: [PATCH] Reserve space for current directory --- src/windows-emulator/emulator_utils.hpp | 18 ++++++++++++------ src/windows-emulator/windows_emulator.cpp | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/windows-emulator/emulator_utils.hpp b/src/windows-emulator/emulator_utils.hpp index ae3cf9b1..fdab413b 100644 --- a/src/windows-emulator/emulator_utils.hpp +++ b/src/windows-emulator/emulator_utils.hpp @@ -187,13 +187,17 @@ class emulator_allocator return reinterpret_cast(uc_str.Buffer); } - void make_unicode_string(UNICODE_STRING>& result, const std::u16string_view str) + void make_unicode_string(UNICODE_STRING>& result, const std::u16string_view str, + const std::optional maximum_length = std::nullopt) { constexpr auto element_size = sizeof(str[0]); constexpr auto required_alignment = alignof(decltype(str[0])); const auto total_length = str.size() * element_size; + const auto total_buffer_length = total_length + element_size; - const auto string_buffer = this->reserve(total_length + element_size, required_alignment); + const auto max_length = std::max(maximum_length.value_or(total_buffer_length), total_buffer_length); + + const auto string_buffer = this->reserve(max_length, required_alignment); this->emu_->write_memory(string_buffer, str.data(), total_length); @@ -202,15 +206,17 @@ class emulator_allocator result.Buffer = string_buffer; result.Length = static_cast(total_length); - result.MaximumLength = static_cast(total_length + element_size); + result.MaximumLength = static_cast(max_length); } - emulator_object>> make_unicode_string(const std::u16string_view str) + emulator_object>> make_unicode_string( + const std::u16string_view str, const std::optional maximum_length = std::nullopt) { const auto unicode_string = this->reserve>>(); - unicode_string.access( - [&](UNICODE_STRING>& unicode_str) { this->make_unicode_string(unicode_str, str); }); + unicode_string.access([&](UNICODE_STRING>& unicode_str) { + this->make_unicode_string(unicode_str, str, maximum_length); // + }); return unicode_string; } diff --git a/src/windows-emulator/windows_emulator.cpp b/src/windows-emulator/windows_emulator.cpp index e09a32b8..72d5f5e2 100644 --- a/src/windows-emulator/windows_emulator.cpp +++ b/src/windows-emulator/windows_emulator.cpp @@ -210,7 +210,7 @@ namespace } void setup_context(windows_emulator& win_emu, const emulator_settings& settings, const windows_path& application, - const windows_path& working_directory) + const windows_path& working_dir) { auto& emu = win_emu.emu(); auto& context = win_emu.process(); @@ -270,7 +270,7 @@ namespace } allocator.make_unicode_string(proc_params.CommandLine, command_line); - allocator.make_unicode_string(proc_params.CurrentDirectory.DosPath, working_directory.u16string() + u"\\"); + allocator.make_unicode_string(proc_params.CurrentDirectory.DosPath, working_dir.u16string() + u"\\", 1024); allocator.make_unicode_string(proc_params.ImagePathName, application_str); const auto total_length = allocator.get_next_address() - context.process_params.value();