Skip to content

Commit 7210216

Browse files
committed
fixup! src: parse inspector profiles with simdjson
1 parent bf36ef9 commit 7210216

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/inspector_profiler.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "v8-inspector.h"
1212

1313
#include <cinttypes>
14+
#include <limits>
1415
#include <sstream>
1516
#include "simdutf.h"
1617

@@ -44,6 +45,10 @@ uint64_t V8ProfilerConnection::DispatchMessage(const char* method,
4445
bool is_profile_request) {
4546
std::stringstream ss;
4647
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()));
4752
ss << R"({ "id": )" << id;
4853
DCHECK(method != nullptr);
4954
ss << R"(, "method": ")" << method << '"';
@@ -83,12 +88,13 @@ static void WriteResult(Environment* env,
8388

8489
bool StringViewToUTF8(const v8_inspector::StringView& source,
8590
std::vector<char>* utf8_out,
86-
size_t* utf8_length) {
91+
size_t* utf8_length,
92+
size_t padding) {
8793
size_t source_len = source.length();
8894
if (source.is8Bit()) {
8995
const char* latin1 = reinterpret_cast<const char*>(source.characters8());
9096
*utf8_length = simdutf::utf8_length_from_latin1(latin1, source_len);
91-
utf8_out->resize(*utf8_length);
97+
utf8_out->resize(*utf8_length + padding);
9298
size_t result_len =
9399
simdutf::convert_latin1_to_utf8(latin1, source_len, utf8_out->data());
94100
return *utf8_length == result_len;
@@ -97,7 +103,7 @@ bool StringViewToUTF8(const v8_inspector::StringView& source,
97103
const char16_t* utf16 =
98104
reinterpret_cast<const char16_t*>(source.characters16());
99105
*utf8_length = simdutf::utf8_length_from_utf16(utf16, source_len);
100-
utf8_out->resize(*utf8_length);
106+
utf8_out->resize(*utf8_length + padding);
101107
size_t result_len =
102108
simdutf::convert_utf16_to_utf8(utf16, source_len, utf8_out->data());
103109
return *utf8_length == result_len;
@@ -119,13 +125,11 @@ void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend(
119125

120126
std::vector<char> message_utf8;
121127
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)) {
123129
fprintf(
124130
stderr, "Failed to convert %s profile message to UTF8 string\n", type);
125131
return;
126132
}
127-
// Allocate extra padding for JSON parsing.
128-
message_utf8.resize(message_utf8_length + simdjson::SIMDJSON_PADDING);
129133

130134
simdjson::ondemand::document parsed;
131135
simdjson::ondemand::object response;

0 commit comments

Comments
 (0)