Skip to content

Commit 1d592a3

Browse files
jasnellmarco-ippolito
authored andcommitted
src: fixup errorhandling more in various places
PR-URL: #57852 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent b6a458c commit 1d592a3

File tree

8 files changed

+41
-20
lines changed

8 files changed

+41
-20
lines changed

src/api/environment.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,11 @@ MaybeLocal<Value> LoadEnvironment(Environment* env,
528528
return LoadEnvironment(
529529
env,
530530
[&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> {
531-
Local<Value> main_script =
532-
ToV8Value(env->context(), main_script_source_utf8).ToLocalChecked();
531+
Local<Value> main_script;
532+
if (!ToV8Value(env->context(), main_script_source_utf8)
533+
.ToLocal(&main_script)) {
534+
return {};
535+
}
533536
return info.run_cjs->Call(
534537
env->context(), Null(env->isolate()), 1, &main_script);
535538
},

src/js_stream.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ int JSStream::DoWrite(WriteWrap* w,
116116
HandleScope scope(env()->isolate());
117117
Context::Scope context_scope(env()->context());
118118

119+
int value_int = UV_EPROTO;
120+
119121
MaybeStackBuffer<Local<Value>, 16> bufs_arr(count);
120122
for (size_t i = 0; i < count; i++) {
121-
bufs_arr[i] =
122-
Buffer::Copy(env(), bufs[i].base, bufs[i].len).ToLocalChecked();
123+
if (!Buffer::Copy(env(), bufs[i].base, bufs[i].len).ToLocal(&bufs_arr[i])) {
124+
return value_int;
125+
}
123126
}
124127

125128
Local<Value> argv[] = {
@@ -129,7 +132,6 @@ int JSStream::DoWrite(WriteWrap* w,
129132

130133
TryCatchScope try_catch(env());
131134
Local<Value> value;
132-
int value_int = UV_EPROTO;
133135
if (!MakeCallback(env()->onwrite_string(),
134136
arraysize(argv),
135137
argv).ToLocal(&value) ||

src/js_udp_wrap.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ ssize_t JSUDPWrap::Send(uv_buf_t* bufs,
9999

100100
MaybeStackBuffer<Local<Value>, 16> buffers(nbufs);
101101
for (size_t i = 0; i < nbufs; i++) {
102-
buffers[i] = Buffer::Copy(env(), bufs[i].base, bufs[i].len)
103-
.ToLocalChecked();
102+
if (!Buffer::Copy(env(), bufs[i].base, bufs[i].len).ToLocal(&buffers[i])) {
103+
return value_int;
104+
}
104105
total_len += bufs[i].len;
105106
}
106107

src/node_contextify.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,12 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
11841184
return false;
11851185
}
11861186

1187-
args.GetReturnValue().Set(result.ToLocalChecked());
1187+
// We checked for res being empty previously so this is a bit redundant
1188+
// but still safer than using ToLocalChecked.
1189+
Local<Value> res;
1190+
if (!result.ToLocal(&res)) return false;
1191+
1192+
args.GetReturnValue().Set(res);
11881193
return true;
11891194
}
11901195

src/node_http_common.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,11 @@ class NgRcBufPointer : public MemoryRetainer {
414414
const char* header_name = reinterpret_cast<const char*>(ptr.data());
415415
v8::Eternal<v8::String>& eternal = static_str_map[header_name];
416416
if (eternal.IsEmpty()) {
417-
v8::Local<v8::String> str =
418-
GetInternalizedString(env, ptr).ToLocalChecked();
417+
v8::Local<v8::String> str;
418+
if (!GetInternalizedString(env, ptr).ToLocal(&str)) {
419+
ptr.reset();
420+
return {};
421+
}
419422
eternal.Set(env->isolate(), str);
420423
return str;
421424
}

src/pipe_wrap.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
5353
EscapableHandleScope handle_scope(env->isolate());
5454
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
5555
CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
56-
Local<Function> constructor = env->pipe_constructor_template()
57-
->GetFunction(env->context())
58-
.ToLocalChecked();
59-
CHECK_EQ(false, constructor.IsEmpty());
56+
Local<Function> constructor;
57+
if (!env->pipe_constructor_template()
58+
->GetFunction(env->context())
59+
.ToLocal(&constructor)) {
60+
return {};
61+
}
6062
Local<Value> type_value = Int32::New(env->isolate(), type);
6163
return handle_scope.EscapeMaybe(
6264
constructor->NewInstance(env->context(), 1, &type_value));

src/tcp_wrap.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
5959
EscapableHandleScope handle_scope(env->isolate());
6060
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
6161
CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false);
62-
Local<Function> constructor = env->tcp_constructor_template()
63-
->GetFunction(env->context())
64-
.ToLocalChecked();
65-
CHECK_EQ(constructor.IsEmpty(), false);
62+
Local<Function> constructor;
63+
if (!env->tcp_constructor_template()
64+
->GetFunction(env->context())
65+
.ToLocal(&constructor)) {
66+
return {};
67+
}
6668
Local<Value> type_value = Int32::New(env->isolate(), type);
6769
return handle_scope.EscapeMaybe(
6870
constructor->NewInstance(env->context(), 1, &type_value));

src/util.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,11 @@ v8::Maybe<int32_t> GetValidatedFd(Environment* env,
791791
const bool is_out_of_range = fd < 0 || fd > INT32_MAX;
792792

793793
if (is_out_of_range || !IsSafeJsInt(input)) {
794-
Utf8Value utf8_value(
795-
env->isolate(), input->ToDetailString(env->context()).ToLocalChecked());
794+
Local<String> str;
795+
if (!input->ToDetailString(env->context()).ToLocal(&str)) {
796+
return v8::Nothing<int32_t>();
797+
}
798+
Utf8Value utf8_value(env->isolate(), str);
796799
if (is_out_of_range && !std::isinf(fd)) {
797800
THROW_ERR_OUT_OF_RANGE(env,
798801
"The value of \"fd\" is out of range. "

0 commit comments

Comments
 (0)