From f388d3370ac876dc0868ca31537acf9a309722b9 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 2 Jun 2019 16:05:31 +0200 Subject: [PATCH] src: use RAII in setgroups implementation Prefer `MaybeStackBuffer` over manual memory management. --- src/node_credentials.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/node_credentials.cc b/src/node_credentials.cc index 6f99cf6641eec3..ad0e1dbb9bb68e 100644 --- a/src/node_credentials.cc +++ b/src/node_credentials.cc @@ -307,14 +307,13 @@ static void SetGroups(const FunctionCallbackInfo& args) { Local groups_list = args[0].As(); size_t size = groups_list->Length(); - gid_t* groups = new gid_t[size]; + MaybeStackBuffer 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(i + 1)); return; @@ -323,8 +322,7 @@ static void SetGroups(const FunctionCallbackInfo& args) { groups[i] = gid; } - int rc = setgroups(size, groups); - delete[] groups; + int rc = setgroups(size, *groups); if (rc == -1) return env->ThrowErrnoException(errno, "setgroups");