Skip to content

Commit

Permalink
src: use RAII in setgroups implementation
Browse files Browse the repository at this point in the history
Prefer `MaybeStackBuffer` over manual memory management.

PR-URL: #28022
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
addaleax authored and BridgeAR committed Jun 17, 2019
1 parent 1ef2811 commit 95ee3b5
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/node_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,13 @@ static void SetGroups(const FunctionCallbackInfo<Value>& args) {

Local<Array> groups_list = args[0].As<Array>();
size_t size = groups_list->Length();
gid_t* groups = new gid_t[size];
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());

if (gid == gid_not_found) {
delete[] groups;
// Tells JS to throw ERR_INVALID_CREDENTIAL
args.GetReturnValue().Set(static_cast<uint32_t>(i + 1));
return;
Expand All @@ -323,8 +322,7 @@ static void SetGroups(const FunctionCallbackInfo<Value>& args) {
groups[i] = gid;
}

int rc = setgroups(size, groups);
delete[] groups;
int rc = setgroups(size, *groups);

if (rc == -1) return env->ThrowErrnoException(errno, "setgroups");

Expand Down

0 comments on commit 95ee3b5

Please sign in to comment.