11
11
#include " v8-inspector.h"
12
12
13
13
#include < cinttypes>
14
+ #include < limits>
14
15
#include < sstream>
15
16
#include " simdutf.h"
16
17
@@ -44,6 +45,10 @@ uint64_t V8ProfilerConnection::DispatchMessage(const char* method,
44
45
bool is_profile_request) {
45
46
std::stringstream ss;
46
47
uint64_t id = next_id ();
48
+ // V8's inspector protocol cannot take an integer beyond the int32_t limit.
49
+ // In practice the id we use is up to 3-5 for the profilers we have
50
+ // here.
51
+ CHECK_LT (id, static_cast <uint64_t >(std::numeric_limits<int32_t >::max ()));
47
52
ss << R"( { "id": )" << id;
48
53
DCHECK (method != nullptr );
49
54
ss << R"( , "method": ")" << method << ' "' ;
@@ -83,12 +88,13 @@ static void WriteResult(Environment* env,
83
88
84
89
bool StringViewToUTF8 (const v8_inspector::StringView& source,
85
90
std::vector<char >* utf8_out,
86
- size_t * utf8_length) {
91
+ size_t * utf8_length,
92
+ size_t padding) {
87
93
size_t source_len = source.length ();
88
94
if (source.is8Bit ()) {
89
95
const char * latin1 = reinterpret_cast <const char *>(source.characters8 ());
90
96
*utf8_length = simdutf::utf8_length_from_latin1 (latin1, source_len);
91
- utf8_out->resize (*utf8_length);
97
+ utf8_out->resize (*utf8_length + padding );
92
98
size_t result_len =
93
99
simdutf::convert_latin1_to_utf8 (latin1, source_len, utf8_out->data ());
94
100
return *utf8_length == result_len;
@@ -97,7 +103,7 @@ bool StringViewToUTF8(const v8_inspector::StringView& source,
97
103
const char16_t * utf16 =
98
104
reinterpret_cast <const char16_t *>(source.characters16 ());
99
105
*utf8_length = simdutf::utf8_length_from_utf16 (utf16, source_len);
100
- utf8_out->resize (*utf8_length);
106
+ utf8_out->resize (*utf8_length + padding );
101
107
size_t result_len =
102
108
simdutf::convert_utf16_to_utf8 (utf16, source_len, utf8_out->data ());
103
109
return *utf8_length == result_len;
@@ -119,13 +125,11 @@ void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend(
119
125
120
126
std::vector<char > message_utf8;
121
127
size_t message_utf8_length;
122
- if (!StringViewToUTF8 (message, &message_utf8, &message_utf8_length)) {
128
+ if (!StringViewToUTF8 (message, &message_utf8, &message_utf8_length, simdjson::SIMDJSON_PADDING )) {
123
129
fprintf (
124
130
stderr, " Failed to convert %s profile message to UTF8 string\n " , type);
125
131
return ;
126
132
}
127
- // Allocate extra padding for JSON parsing.
128
- message_utf8.resize (message_utf8_length + simdjson::SIMDJSON_PADDING);
129
133
130
134
simdjson::ondemand::document parsed;
131
135
simdjson::ondemand::object response;
0 commit comments