@@ -27,15 +27,15 @@ using namespace lldb_private;
2727// / extra L'\0' (two bytes of 0). An empty environment must have one
2828// / empty string, followed by an extra L'\0'.
2929// /
30- // / The keys are sorted to comply with the CreateProcess' calling convention.
30+ // / The keys are sorted to comply with the CreateProcess API calling convention.
3131// /
3232// / Ensure that the resulting buffer is used in conjunction with
3333// / CreateProcessW and be sure that dwCreationFlags includes
3434// / CREATE_UNICODE_ENVIRONMENT.
3535// /
3636// / \param env The Environment object to convert.
3737// / \returns The sorted sequence of environment variables and their values,
38- // / separated by null terminators.
38+ // / separated by null terminators. The vector is guaranteed to never be empty.
3939static std::vector<wchar_t > CreateEnvironmentBufferW (const Environment &env) {
4040 std::vector<std::wstring> env_entries;
4141 for (const auto &KV : env) {
@@ -53,6 +53,10 @@ static std::vector<wchar_t> CreateEnvironmentBufferW(const Environment &env) {
5353 buffer.insert (buffer.end (), env_entry.begin (), env_entry.end ());
5454 buffer.push_back (L' \0 ' );
5555 }
56+
57+ if (buffer.empty ())
58+ buffer.push_back (L' \0 ' ); // If there are no environment variables, we have
59+ // to ensure there are 4 zero bytes in the buffer.
5660 buffer.push_back (L' \0 ' );
5761
5862 return buffer;
@@ -167,7 +171,6 @@ ProcessLauncherWindows::LaunchProcess(const ProcessLaunchInfo &launch_info,
167171
168172 std::vector<wchar_t > environment =
169173 CreateEnvironmentBufferW (launch_info.GetEnvironment ());
170- LPVOID env_block = environment.empty () ? nullptr : environment.data ();
171174
172175 auto wcommandLineOrErr =
173176 GetFlattenedWindowsCommandStringW (launch_info.GetArguments ());
@@ -189,7 +192,7 @@ ProcessLauncherWindows::LaunchProcess(const ProcessLaunchInfo &launch_info,
189192
190193 BOOL result = ::CreateProcessW (
191194 wexecutable.c_str (), pwcommandLine, NULL , NULL ,
192- /* bInheritHandles=*/ !inherited_handles.empty (), flags, env_block ,
195+ /* bInheritHandles=*/ !inherited_handles.empty (), flags, environment. data () ,
193196 wworkingDirectory.size () == 0 ? NULL : wworkingDirectory.c_str (),
194197 reinterpret_cast <STARTUPINFOW *>(&startupinfoex), &pi);
195198
0 commit comments