Skip to content

Commit dc00bf8

Browse files
klemens-morgensternKlemens Morgenstern
authored andcommitted
Fixes args & inherited handles.
1 parent 47b5c3c commit dc00bf8

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

include/boost/process/v2/detail/utf8.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ std::basic_string<CharOut, Traits, Allocator> conv_string(
4444
if (ec)
4545
detail::throw_error(ec, "size_as_utf8");
4646

47+
4748
std::basic_string<CharOut, Traits, Allocator> res(allocator);
4849
res.resize(req_size);
4950

51+
if (req_size == 0)
52+
return res;
53+
54+
5055
auto res_size = convert_to_utf8(data, size, &res.front(), req_size, ec);
5156
if (ec)
5257
detail::throw_error(ec, "convert_to_utf8");
@@ -70,6 +75,9 @@ std::basic_string<CharOut, Traits, Allocator> conv_string(
7075
std::basic_string<CharOut, Traits, Allocator> res(allocator);
7176
res.resize(req_size);
7277

78+
if (req_size == 0)
79+
return res;
80+
7381
auto res_size = convert_to_wide(data, size, &res.front(), req_size, ec);
7482
if (ec)
7583
detail::throw_error(ec, "convert_to_wide");

include/boost/process/v2/windows/default_launcher.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ struct default_launcher
352352
BOOST_PROCESS_V2_DECL static
353353
std::size_t escape_argv_string(wchar_t * itr, std::size_t max_size,
354354
basic_string_view<wchar_t> ws);
355-
356355

357356

358357

@@ -395,6 +394,7 @@ struct default_launcher
395394
{
396395
return detail::conv_string<wchar_t>(arg.data(), arg.size());
397396
});
397+
398398
return build_command_line_impl(pt, argw, L"");
399399
}
400400

@@ -406,10 +406,11 @@ struct default_launcher
406406
{
407407
std::wstring buffer;
408408
buffer.resize(escaped_argv_length(pt.native()));
409-
escape_argv_string(&buffer.front(), buffer.size(), pt.native());
409+
410+
if (!buffer.empty())
411+
escape_argv_string(&buffer.front(), buffer.size(), pt.native());
410412
return buffer;
411413
}
412-
413414
return build_command_line_impl(pt, args, *std::begin(args));
414415
}
415416

@@ -438,4 +439,4 @@ BOOST_PROCESS_V2_END_NAMESPACE
438439

439440

440441

441-
#endif //BOOST_PROCESS_V2_WINDOWS_DEFAULT_LAUNCHER_HPP
442+
#endif //BOOST_PROCESS_V2_WINDOWS_DEFAULT_LAUNCHER_HPP

src/windows/default_launcher.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ namespace windows
3030
std::size_t needed_escapes = 0u;
3131
for (auto itr = ws.begin(); itr != ws.end(); itr ++)
3232
{
33-
if (*itr == L' ')
33+
if (*itr == quote)
3434
needed_escapes++;
3535
else if (*itr == L'\\')
3636
{
3737
auto nx = std::next(itr);
38-
if (nx != ws.end() && (*nx == L' ' || *nx == L'\t'))
38+
if (nx != ws.end() && *nx == L'"')
3939
needed_escapes ++;
40-
else if (nx == ws.end() && needs_quotes)
40+
else if (nx == ws.end())
4141
needed_escapes ++;
4242
}
4343
}
@@ -75,17 +75,16 @@ namespace windows
7575
if (*it == quote) // makes it \"
7676
*(itr++) = L'\\';
7777

78-
if (*it == L'\\') // \" needs to be come \\\"
78+
if (*it == L'\\') // \" needs to become \\\"
7979
{
8080
auto nx = std::next(it);
81-
if (nx != ws.end() && (*nx == L' ' || *nx == L'\t'))
81+
if (nx != ws.end() && *nx == L'"')
8282
*(itr++) = L'\\';
83-
else if (nx == ws.end() && needs_quotes)
84-
{
83+
else if (nx == ws.end())
8584
*(itr++) = L'\\';
86-
}
85+
8786
}
88-
87+
8988
*(itr++) = *it;
9089
}
9190

@@ -127,9 +126,13 @@ namespace windows
127126
auto tl = get_thread_attribute_list(ec);
128127
if (ec)
129128
return;
129+
130+
auto itr = std::unique(inherited_handles.begin(), inherited_handles.end());
131+
auto size = std::distance(inherited_handles.begin(), itr);
132+
130133
if (!::UpdateProcThreadAttribute(
131134
tl, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
132-
inherited_handles.data(), inherited_handles.size() * sizeof(HANDLE), nullptr, nullptr))
135+
inherited_handles.data(), size * sizeof(HANDLE), nullptr, nullptr))
133136
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec);
134137
}
135138

test/v2/process.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,11 @@ BOOST_AUTO_TEST_CASE(print_args_spec_out)
256256
asio::writable_pipe wp{ctx};
257257
asio::connect_pipe(rp, wp);
258258

259-
260-
bpv::process proc(ctx, pth, {"print-args", "&foo", "&", "", "\"\"", "\\\"", "|bar", "\"", "#foobar"}, bpv::process_stdio{/*in*/{},/*out*/wp, /*err*/ nullptr});
259+
fprintf(stderr, "print_args_spec_out\n");
260+
261+
bpv::process proc(ctx, pth, {"print-args", "&foo", "&", "", "\"\"", "\\\"", "|bar", "\"", "#foobar"},
262+
bpv::process_stdio{/*in*/{},/*out*/wp, /*err*/ nullptr});
263+
BOOST_CHECK(proc.running());
261264

262265
wp.close();
263266
asio::streambuf st;
@@ -877,8 +880,8 @@ BOOST_AUTO_TEST_CASE(print_args_combined)
877880
asio::connect_pipe(rp, wp);
878881

879882
bpv::process proc(ctx, pth, {"print-args", "bar", "foo"}, bpv::process_stdio{/*in*/{}, /*.out= */ wp, /* .err=*/ wp});
880-
881883
wp.close();
884+
882885
asio::streambuf st;
883886
std::istream is{&st};
884887
bpv::error_code ec;

0 commit comments

Comments
 (0)