Skip to content

Commit 436c3ae

Browse files
kvakilUlisesGascon
authored andcommitted
src: fix JSONParser leaking internal V8 scopes
JSONParser uses V8's JSON.parse (for now), meaning that its uses handles and contexts. JSONParser was leaking its internal HandleScope and Context::Scope. Move the scope construction to the member functions to prevent those scopes from leaking. Refs: #50680 (comment) PR-URL: #50688 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 3d82d38 commit 436c3ae

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/json_parser.cc

+17-5
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ using v8::Object;
1111
using v8::String;
1212
using v8::Value;
1313

14-
JSONParser::JSONParser()
15-
: handle_scope_(isolate_.get()),
16-
context_(isolate_.get(), Context::New(isolate_.get())),
17-
context_scope_(context_.Get(isolate_.get())) {}
14+
JSONParser::JSONParser() {}
1815

1916
bool JSONParser::Parse(const std::string& content) {
2017
DCHECK(!parsed_);
2118

2219
Isolate* isolate = isolate_.get();
23-
Local<Context> context = context_.Get(isolate);
20+
v8::HandleScope handle_scope(isolate);
21+
22+
Local<Context> context = Context::New(isolate);
23+
Context::Scope context_scope(context);
2424

2525
// It's not a real script, so don't print the source line.
2626
errors::PrinterTryCatch bootstrapCatch(
@@ -34,16 +34,24 @@ bool JSONParser::Parse(const std::string& content) {
3434
!result_value->IsObject()) {
3535
return false;
3636
}
37+
38+
context_.Reset(isolate, context);
3739
content_.Reset(isolate, result_value.As<Object>());
3840
parsed_ = true;
41+
3942
return true;
4043
}
4144

4245
std::optional<std::string> JSONParser::GetTopLevelStringField(
4346
std::string_view field) {
4447
Isolate* isolate = isolate_.get();
48+
v8::HandleScope handle_scope(isolate);
49+
4550
Local<Context> context = context_.Get(isolate);
51+
Context::Scope context_scope(context);
52+
4653
Local<Object> content_object = content_.Get(isolate);
54+
4755
Local<Value> value;
4856
// It's not a real script, so don't print the source line.
4957
errors::PrinterTryCatch bootstrapCatch(
@@ -62,7 +70,11 @@ std::optional<std::string> JSONParser::GetTopLevelStringField(
6270

6371
std::optional<bool> JSONParser::GetTopLevelBoolField(std::string_view field) {
6472
Isolate* isolate = isolate_.get();
73+
v8::HandleScope handle_scope(isolate);
74+
6575
Local<Context> context = context_.Get(isolate);
76+
Context::Scope context_scope(context);
77+
6678
Local<Object> content_object = content_.Get(isolate);
6779
Local<Value> value;
6880
bool has_field;

src/json_parser.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ class JSONParser {
2525
// We might want a lighter-weight JSON parser for this use case. But for now
2626
// using V8 is good enough.
2727
RAIIIsolate isolate_;
28-
v8::HandleScope handle_scope_;
28+
2929
v8::Global<v8::Context> context_;
30-
v8::Context::Scope context_scope_;
3130
v8::Global<v8::Object> content_;
3231
bool parsed_ = false;
3332
};

0 commit comments

Comments
 (0)