Skip to content

src: fixup errorhandling more in various places #57852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/encoding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ MaybeLocal<Value> TryEncode(Isolate* isolate,
}

MaybeLocal<Value> TryEncode(Isolate* isolate, const uint16_t* buf, size_t len) {
return StringBytes::Encode(isolate, buf, len).ToLocalChecked();
return StringBytes::Encode(isolate, buf, len);
}

Local<Value> Encode(Isolate* isolate,
Expand Down
7 changes: 5 additions & 2 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,11 @@ MaybeLocal<Value> LoadEnvironment(Environment* env,
return LoadEnvironment(
env,
[&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> {
Local<Value> main_script =
ToV8Value(env->context(), main_script_source_utf8).ToLocalChecked();
Local<Value> main_script;
if (!ToV8Value(env->context(), main_script_source_utf8)
.ToLocal(&main_script)) {
return {};
}
return info.run_cjs->Call(
env->context(), Null(env->isolate()), 1, &main_script);
},
Expand Down
8 changes: 5 additions & 3 deletions src/js_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,13 @@ int JSStream::DoWrite(WriteWrap* w,
HandleScope scope(env()->isolate());
Context::Scope context_scope(env()->context());

int value_int = UV_EPROTO;

MaybeStackBuffer<Local<Value>, 16> bufs_arr(count);
for (size_t i = 0; i < count; i++) {
bufs_arr[i] =
Buffer::Copy(env(), bufs[i].base, bufs[i].len).ToLocalChecked();
if (!Buffer::Copy(env(), bufs[i].base, bufs[i].len).ToLocal(&bufs_arr[i])) {
return value_int;
}
}

Local<Value> argv[] = {
Expand All @@ -130,7 +133,6 @@ int JSStream::DoWrite(WriteWrap* w,

TryCatchScope try_catch(env());
Local<Value> value;
int value_int = UV_EPROTO;
if (!MakeCallback(env()->onwrite_string(),
arraysize(argv),
argv).ToLocal(&value) ||
Expand Down
5 changes: 3 additions & 2 deletions src/js_udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ ssize_t JSUDPWrap::Send(uv_buf_t* bufs,

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

Expand Down
7 changes: 6 additions & 1 deletion src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,12 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
return false;
}

args.GetReturnValue().Set(result.ToLocalChecked());
// We checked for res being empty previously so this is a bit redundant
// but still safer than using ToLocalChecked.
Local<Value> res;
if (!result.ToLocal(&res)) return false;

args.GetReturnValue().Set(res);
return true;
}

Expand Down
7 changes: 5 additions & 2 deletions src/node_http_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,11 @@ class NgRcBufPointer : public MemoryRetainer {
const char* header_name = reinterpret_cast<const char*>(ptr.data());
v8::Eternal<v8::String>& eternal = static_str_map[header_name];
if (eternal.IsEmpty()) {
v8::Local<v8::String> str =
GetInternalizedString(env, ptr).ToLocalChecked();
v8::Local<v8::String> str;
if (!GetInternalizedString(env, ptr).ToLocal(&str)) {
ptr.reset();
return {};
}
eternal.Set(env->isolate(), str);
return str;
}
Expand Down
10 changes: 6 additions & 4 deletions src/pipe_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
EscapableHandleScope handle_scope(env->isolate());
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
Local<Function> constructor = env->pipe_constructor_template()
->GetFunction(env->context())
.ToLocalChecked();
CHECK_EQ(false, constructor.IsEmpty());
Local<Function> constructor;
if (!env->pipe_constructor_template()
->GetFunction(env->context())
.ToLocal(&constructor)) {
return {};
}
Local<Value> type_value = Int32::New(env->isolate(), type);
return handle_scope.EscapeMaybe(
constructor->NewInstance(env->context(), 1, &type_value));
Expand Down
10 changes: 6 additions & 4 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
EscapableHandleScope handle_scope(env->isolate());
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false);
Local<Function> constructor = env->tcp_constructor_template()
->GetFunction(env->context())
.ToLocalChecked();
CHECK_EQ(constructor.IsEmpty(), false);
Local<Function> constructor;
if (!env->tcp_constructor_template()
->GetFunction(env->context())
.ToLocal(&constructor)) {
return {};
}
Local<Value> type_value = Int32::New(env->isolate(), type);
return handle_scope.EscapeMaybe(
constructor->NewInstance(env->context(), 1, &type_value));
Expand Down
7 changes: 5 additions & 2 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,11 @@ v8::Maybe<int32_t> GetValidatedFd(Environment* env,
const bool is_out_of_range = fd < 0 || fd > INT32_MAX;

if (is_out_of_range || !IsSafeJsInt(input)) {
Utf8Value utf8_value(
env->isolate(), input->ToDetailString(env->context()).ToLocalChecked());
Local<String> str;
if (!input->ToDetailString(env->context()).ToLocal(&str)) {
return v8::Nothing<int32_t>();
}
Utf8Value utf8_value(env->isolate(), str);
if (is_out_of_range && !std::isinf(fd)) {
THROW_ERR_OUT_OF_RANGE(env,
"The value of \"fd\" is out of range. "
Expand Down
Loading