@@ -49,15 +49,15 @@ static std::vector<winrt::hstring> commandlineToArgArray(const wchar_t* commandL
49
49
}
50
50
51
51
// Returns the length of a double-null encoded string *excluding* the trailing double-null character.
52
- static std::wstring_view stringFromDoubleNullTerminated (const wchar_t * beg)
52
+ static wil::zwstring_view stringFromDoubleNullTerminated (const wchar_t * beg)
53
53
{
54
54
auto end = beg;
55
55
56
56
for (; *end; end += wcsnlen (end, SIZE_T_MAX) + 1 )
57
57
{
58
58
}
59
59
60
- return { beg, end };
60
+ return { beg, gsl::narrow_cast< size_t >( end - beg) };
61
61
}
62
62
63
63
// Appends an uint32_t to a byte vector.
@@ -78,36 +78,39 @@ static const uint8_t* deserializeUint32(const uint8_t* it, const uint8_t* end, u
78
78
return it + sizeof (uint32_t );
79
79
}
80
80
81
- // Writes an uint32_t length prefix, followed by the string data, to the output vector.
82
- static void serializeString (std::vector<uint8_t >& out, std::wstring_view str)
81
+ // Writes a null-terminated string to `out`: A uint32_t length prefix,
82
+ // *including null byte*, followed by the string data, followed by the null-terminator.
83
+ static void serializeString (std::vector<uint8_t >& out, wil::zwstring_view str)
83
84
{
84
85
const auto ptr = reinterpret_cast <const uint8_t *>(str.data ());
85
- const auto len = gsl::narrow< uint32_t >( str.size ()) ;
86
- serializeUint32 (out, len);
86
+ const auto len = str.size () + 1 ;
87
+ serializeUint32 (out, gsl::narrow< uint32_t >( len) );
87
88
out.insert (out.end (), ptr, ptr + len * sizeof (wchar_t ));
88
89
}
89
90
90
- // Parses the next string from the input iterator . Performs bounds-checks.
91
+ // Counter-part to `serializeString` . Performs bounds-checks.
91
92
// Returns an iterator that points past it.
92
- static const uint8_t * deserializeString (const uint8_t * it, const uint8_t * end, std::wstring_view & str)
93
+ static const uint8_t * deserializeString (const uint8_t * it, const uint8_t * end, wil::zwstring_view & str)
93
94
{
94
95
uint32_t len;
95
96
it = deserializeUint32 (it, end, len);
96
97
97
- if (static_cast <size_t >(end - it) < len * sizeof (wchar_t ))
98
+ const auto bytes = static_cast <size_t >(len) * sizeof (wchar_t );
99
+
100
+ if (bytes == 0 || static_cast <size_t >(end - it) < bytes)
98
101
{
99
102
throw std::out_of_range (" Not enough data for string content" );
100
103
}
101
104
102
- str = { reinterpret_cast <const wchar_t *>(it), len };
103
- return it + len * sizeof ( wchar_t ) ;
105
+ str = { reinterpret_cast <const wchar_t *>(it), len - 1 };
106
+ return it + bytes ;
104
107
}
105
108
106
109
struct Handoff
107
110
{
108
- std::wstring_view args;
109
- std::wstring_view env;
110
- std::wstring_view cwd;
111
+ wil::zwstring_view args;
112
+ wil::zwstring_view env;
113
+ wil::zwstring_view cwd;
111
114
uint32_t show;
112
115
};
113
116
@@ -610,7 +613,7 @@ void WindowEmperor::_dispatchCommandline(winrt::TerminalApp::CommandlineArgs arg
610
613
}
611
614
}
612
615
613
- void WindowEmperor::_dispatchCommandlineCommon (winrt::array_view<const winrt::hstring> args, std::wstring_view currentDirectory, std::wstring_view envString, uint32_t showWindowCommand)
616
+ void WindowEmperor::_dispatchCommandlineCommon (winrt::array_view<const winrt::hstring> args, wil::zwstring_view currentDirectory, wil::zwstring_view envString, uint32_t showWindowCommand)
614
617
{
615
618
winrt::TerminalApp::CommandlineArgs c;
616
619
c.Commandline (args);
@@ -927,8 +930,7 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
927
930
if (const auto cds = reinterpret_cast <COPYDATASTRUCT*>(lParam); cds->dwData == TERMINAL_HANDOFF_MAGIC)
928
931
{
929
932
const auto handoff = deserializeHandoffPayload (static_cast <const uint8_t *>(cds->lpData ), static_cast <const uint8_t *>(cds->lpData ) + cds->cbData );
930
- const winrt::hstring args{ handoff.args };
931
- const auto argv = commandlineToArgArray (args.c_str ());
933
+ const auto argv = commandlineToArgArray (handoff.args .c_str ());
932
934
_dispatchCommandlineCommon (argv, handoff.cwd , handoff.env , handoff.show );
933
935
}
934
936
return 0 ;
0 commit comments