Skip to content

Commit 636defd

Browse files
committed
src,stream: remove *Check*() calls from non-Initialize() functions
There is no need to crash the process if any of these checks fail. Signed-off-by: Darshan Sen <[email protected]>
1 parent a784258 commit 636defd

File tree

4 files changed

+77
-40
lines changed

4 files changed

+77
-40
lines changed

src/stream_base-inl.h

+16-10
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
149149

150150
const char* msg = Error();
151151
if (msg != nullptr) {
152-
req_wrap_obj->Set(
153-
env->context(),
154-
env->error_string(), OneByteString(env->isolate(), msg)).Check();
152+
if (req_wrap_obj->Set(env->context(),
153+
env->error_string(),
154+
OneByteString(env->isolate(), msg)).IsNothing()) {
155+
return 0;
156+
}
155157
ClearError();
156158
}
157159

@@ -203,9 +205,11 @@ StreamWriteResult StreamBase::Write(
203205

204206
const char* msg = Error();
205207
if (msg != nullptr) {
206-
req_wrap_obj->Set(env->context(),
207-
env->error_string(),
208-
OneByteString(env->isolate(), msg)).Check();
208+
if (req_wrap_obj->Set(env->context(),
209+
env->error_string(),
210+
OneByteString(env->isolate(), msg)).IsNothing()) {
211+
return StreamWriteResult {};
212+
}
209213
ClearError();
210214
}
211215

@@ -279,10 +283,12 @@ void StreamReq::Done(int status, const char* error_str) {
279283
Environment* env = async_wrap->env();
280284
if (error_str != nullptr) {
281285
v8::HandleScope handle_scope(env->isolate());
282-
async_wrap->object()->Set(env->context(),
283-
env->error_string(),
284-
OneByteString(env->isolate(), error_str))
285-
.Check();
286+
if (async_wrap->object()->Set(
287+
env->context(),
288+
env->error_string(),
289+
OneByteString(env->isolate(), error_str)).IsNothing()) {
290+
return;
291+
}
286292
}
287293

288294
OnDone(status);

src/stream_base.cc

+33-15
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,22 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
106106
if (!all_buffers) {
107107
// Determine storage size first
108108
for (size_t i = 0; i < count; i++) {
109-
Local<Value> chunk = chunks->Get(context, i * 2).ToLocalChecked();
109+
Local<Value> chunk;
110+
if (!chunks->Get(context, i * 2).ToLocal(&chunk))
111+
return 0;
110112

111113
if (Buffer::HasInstance(chunk))
112114
continue;
113115
// Buffer chunk, no additional storage required
114116

115117
// String chunk
116-
Local<String> string = chunk->ToString(context).ToLocalChecked();
117-
enum encoding encoding = ParseEncoding(isolate,
118-
chunks->Get(context, i * 2 + 1).ToLocalChecked());
118+
Local<String> string;
119+
if (!chunk->ToString(context).ToLocal(&string))
120+
return 0;
121+
Local<Value> next_chunk;
122+
if (!chunks->Get(context, i * 2 + 1).ToLocal(&next_chunk))
123+
return 0;
124+
enum encoding encoding = ParseEncoding(isolate, next_chunk);
119125
size_t chunk_size;
120126
if (encoding == UTF8 && string->Length() > 65535 &&
121127
!StringBytes::Size(isolate, string, encoding).To(&chunk_size))
@@ -130,7 +136,9 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
130136
return UV_ENOBUFS;
131137
} else {
132138
for (size_t i = 0; i < count; i++) {
133-
Local<Value> chunk = chunks->Get(context, i).ToLocalChecked();
139+
Local<Value> chunk;
140+
if (!chunks->Get(context, i).ToLocal(&chunk))
141+
return 0;
134142
bufs[i].base = Buffer::Data(chunk);
135143
bufs[i].len = Buffer::Length(chunk);
136144
}
@@ -145,7 +153,9 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
145153
offset = 0;
146154
if (!all_buffers) {
147155
for (size_t i = 0; i < count; i++) {
148-
Local<Value> chunk = chunks->Get(context, i * 2).ToLocalChecked();
156+
Local<Value> chunk;
157+
if (!chunks->Get(context, i * 2).ToLocal(&chunk))
158+
return 0;
149159

150160
// Write buffer
151161
if (Buffer::HasInstance(chunk)) {
@@ -160,9 +170,13 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
160170
static_cast<char*>(bs ? bs->Data() : nullptr) + offset;
161171
size_t str_size = (bs ? bs->ByteLength() : 0) - offset;
162172

163-
Local<String> string = chunk->ToString(context).ToLocalChecked();
164-
enum encoding encoding = ParseEncoding(isolate,
165-
chunks->Get(context, i * 2 + 1).ToLocalChecked());
173+
Local<String> string;
174+
if (!chunk->ToString(context).ToLocal(&string))
175+
return 0;
176+
Local<Value> next_chunk;
177+
if (!chunks->Get(context, i * 2 + 1).ToLocal(&next_chunk))
178+
return 0;
179+
enum encoding encoding = ParseEncoding(isolate, next_chunk);
166180
str_size = StringBytes::Write(isolate,
167181
str_storage,
168182
str_size,
@@ -207,9 +221,11 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
207221
send_handle = reinterpret_cast<uv_stream_t*>(wrap->GetHandle());
208222
// Reference LibuvStreamWrap instance to prevent it from being garbage
209223
// collected before `AfterWrite` is called.
210-
req_wrap_obj->Set(env->context(),
211-
env->handle_string(),
212-
send_handle_obj).Check();
224+
if (req_wrap_obj->Set(env->context(),
225+
env->handle_string(),
226+
send_handle_obj).IsNothing()) {
227+
return 0;
228+
}
213229
}
214230

215231
StreamWriteResult res = Write(&buf, 1, send_handle, req_wrap_obj);
@@ -312,9 +328,11 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
312328
send_handle = reinterpret_cast<uv_stream_t*>(wrap->GetHandle());
313329
// Reference LibuvStreamWrap instance to prevent it from being garbage
314330
// collected before `AfterWrite` is called.
315-
req_wrap_obj->Set(env->context(),
316-
env->handle_string(),
317-
send_handle_obj).Check();
331+
if (req_wrap_obj->Set(env->context(),
332+
env->handle_string(),
333+
send_handle_obj).IsNothing()) {
334+
return 0;
335+
}
318336
}
319337

320338
StreamWriteResult res = Write(&buf, 1, send_handle, req_wrap_obj);

src/stream_pipe.cc

+22-9
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,26 @@ StreamPipe::StreamPipe(StreamBase* source,
3333
// In particular, this makes sure that they are garbage collected as a group,
3434
// if that applies to the given streams (for example, Http2Streams use
3535
// weak references).
36-
obj->Set(env()->context(), env()->source_string(), source->GetObject())
37-
.Check();
38-
source->GetObject()->Set(env()->context(), env()->pipe_target_string(), obj)
39-
.Check();
40-
obj->Set(env()->context(), env()->sink_string(), sink->GetObject())
41-
.Check();
42-
sink->GetObject()->Set(env()->context(), env()->pipe_source_string(), obj)
43-
.Check();
36+
if (obj->Set(env()->context(),
37+
env()->source_string(),
38+
source->GetObject()).IsNothing()) {
39+
return;
40+
}
41+
if (source->GetObject()->Set(env()->context(),
42+
env()->pipe_target_string(),
43+
obj).IsNothing()) {
44+
return;
45+
}
46+
if (obj->Set(env()->context(),
47+
env()->sink_string(),
48+
sink->GetObject()).IsNothing()) {
49+
return;
50+
}
51+
if (sink->GetObject()->Set(env()->context(),
52+
env()->pipe_source_string(),
53+
obj).IsNothing()) {
54+
return;
55+
}
4456
}
4557

4658
StreamPipe::~StreamPipe() {
@@ -172,7 +184,8 @@ void StreamPipe::WritableListener::OnStreamAfterWrite(WriteWrap* w,
172184
Environment* env = pipe->env();
173185
HandleScope handle_scope(env->isolate());
174186
Context::Scope context_scope(env->context());
175-
pipe->MakeCallback(env->oncomplete_string(), 0, nullptr).ToLocalChecked();
187+
if (pipe->MakeCallback(env->oncomplete_string(), 0, nullptr).IsEmpty())
188+
return;
176189
stream()->RemoveStreamListener(this);
177190
}
178191
return;

src/stream_wrap.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,12 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
268268
CHECK_EQ(type, UV_UNKNOWN_HANDLE);
269269
}
270270

271-
if (!pending_obj.IsEmpty()) {
272-
object()
273-
->Set(env()->context(),
274-
env()->pending_handle_string(),
275-
pending_obj.ToLocalChecked())
276-
.Check();
271+
Local<Object> local_pending_obj;
272+
if (pending_obj.ToLocal(&local_pending_obj) &&
273+
object()->Set(env()->context(),
274+
env()->pending_handle_string(),
275+
local_pending_obj).IsNothing()) {
276+
return;
277277
}
278278
}
279279

0 commit comments

Comments
 (0)