Skip to content
Permalink
Browse files
src: use RAII in setgroups implementation
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 95ee3b55d34dee150914bd87f054f55a4d5fb8ee
Showing with 2 additions and 4 deletions.
  1. +2 −4 src/node_credentials.cc
@@ -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;
@@ -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");

0 comments on commit 95ee3b5

Please sign in to comment.