Skip to content

Commit

Permalink
src: improve error handling in node_credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Feb 4, 2025
1 parent efd405f commit af38a30
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/node_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ static void SafeGetenv(const FunctionCallbackInfo<Value>& args) {
Utf8Value strenvtag(isolate, args[0]);
std::string text;
if (!SafeGetenv(*strenvtag, &text, env)) return;
Local<Value> result =
ToV8Value(isolate->GetCurrentContext(), text).ToLocalChecked();
args.GetReturnValue().Set(result);
Local<Value> result;
if (ToV8Value(isolate->GetCurrentContext(), text).ToLocal(&result)) {
args.GetReturnValue().Set(result);
}
}

static void GetTempDir(const FunctionCallbackInfo<Value>& args) {
Expand All @@ -137,8 +138,10 @@ static void GetTempDir(const FunctionCallbackInfo<Value>& args) {
dir.pop_back();
}

args.GetReturnValue().Set(
ToV8Value(isolate->GetCurrentContext(), dir).ToLocalChecked());
Local<Value> result;
if (ToV8Value(isolate->GetCurrentContext(), dir).ToLocal(&result)) {
args.GetReturnValue().Set(result);
}
}

#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
Expand Down Expand Up @@ -385,9 +388,10 @@ static void GetGroups(const FunctionCallbackInfo<Value>& args) {
gid_t egid = getegid();
if (std::find(groups.begin(), groups.end(), egid) == groups.end())
groups.push_back(egid);
MaybeLocal<Value> array = ToV8Value(env->context(), groups);
if (!array.IsEmpty())
args.GetReturnValue().Set(array.ToLocalChecked());
Local<Value> result;
if (ToV8Value(env->context(), groups).ToLocal(&result)) {
args.GetReturnValue().Set(result);
}
}

static void SetGroups(const FunctionCallbackInfo<Value>& args) {
Expand All @@ -403,8 +407,12 @@ static void SetGroups(const FunctionCallbackInfo<Value>& args) {
MaybeStackBuffer<gid_t, 64> groups(size);

for (size_t i = 0; i < size; i++) {
gid_t gid = gid_by_name(
env->isolate(), groups_list->Get(env->context(), i).ToLocalChecked());
Local<Value> val;
if (!groups_list->Get(env->context(), i).ToLocal(&val)) {
// V8 will have scheduled an error to be thrown.
return;
}
gid_t gid = gid_by_name(env->isolate(), val);

if (gid == gid_not_found) {
// Tells JS to throw ERR_INVALID_CREDENTIAL
Expand Down

0 comments on commit af38a30

Please sign in to comment.