Skip to content

Commit b779c07

Browse files
committed
src: make StreamPipe::Unpipe() more resilient
Clean up `StreamPipe::Unpipe()` to be more resilient against unexpected exceptions, in particular while executing its `MakeCallback()` line (which can fail in the presence of termination exceptions), and clean up the getter/setter part of the code to match that pattern as well (even though it should not fail as part of regular operations). PR-URL: #25716 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
1 parent 0b014d5 commit b779c07

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/stream_pipe.cc

+23-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using v8::Context;
66
using v8::External;
7+
using v8::Function;
78
using v8::FunctionCallbackInfo;
89
using v8::FunctionTemplate;
910
using v8::Local;
@@ -77,30 +78,35 @@ void StreamPipe::Unpipe() {
7778
Context::Scope context_scope(env->context());
7879
Local<Object> object = pipe->object();
7980

80-
if (object->Has(env->context(), env->onunpipe_string()).FromJust()) {
81-
pipe->MakeCallback(env->onunpipe_string(), 0, nullptr).ToLocalChecked();
81+
Local<Value> onunpipe;
82+
if (!object->Get(env->context(), env->onunpipe_string()).ToLocal(&onunpipe))
83+
return;
84+
if (onunpipe->IsFunction() &&
85+
pipe->MakeCallback(onunpipe.As<Function>(), 0, nullptr).IsEmpty()) {
86+
return;
8287
}
8388

8489
// Set all the links established in the constructor to `null`.
8590
Local<Value> null = Null(env->isolate());
8691

8792
Local<Value> source_v;
8893
Local<Value> sink_v;
89-
source_v = object->Get(env->context(), env->source_string())
90-
.ToLocalChecked();
91-
sink_v = object->Get(env->context(), env->sink_string())
92-
.ToLocalChecked();
93-
CHECK(source_v->IsObject());
94-
CHECK(sink_v->IsObject());
95-
96-
object->Set(env->context(), env->source_string(), null).FromJust();
97-
object->Set(env->context(), env->sink_string(), null).FromJust();
98-
source_v.As<Object>()->Set(env->context(),
99-
env->pipe_target_string(),
100-
null).FromJust();
101-
sink_v.As<Object>()->Set(env->context(),
102-
env->pipe_source_string(),
103-
null).FromJust();
94+
if (!object->Get(env->context(), env->source_string()).ToLocal(&source_v) ||
95+
!object->Get(env->context(), env->sink_string()).ToLocal(&sink_v) ||
96+
!source_v->IsObject() || !sink_v->IsObject()) {
97+
return;
98+
}
99+
100+
if (object->Set(env->context(), env->source_string(), null).IsNothing() ||
101+
object->Set(env->context(), env->sink_string(), null).IsNothing() ||
102+
source_v.As<Object>()
103+
->Set(env->context(), env->pipe_target_string(), null)
104+
.IsNothing() ||
105+
sink_v.As<Object>()
106+
->Set(env->context(), env->pipe_source_string(), null)
107+
.IsNothing()) {
108+
return;
109+
}
104110
}, static_cast<void*>(this), object());
105111
}
106112

0 commit comments

Comments
 (0)