Skip to content

Commit 95ee3b5

Browse files
addaleaxBridgeAR
authored andcommitted
src: use RAII in setgroups implementation
Prefer `MaybeStackBuffer` over manual memory management. PR-URL: #28022 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 1ef2811 commit 95ee3b5

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

src/node_credentials.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,13 @@ static void SetGroups(const FunctionCallbackInfo<Value>& args) {
307307

308308
Local<Array> groups_list = args[0].As<Array>();
309309
size_t size = groups_list->Length();
310-
gid_t* groups = new gid_t[size];
310+
MaybeStackBuffer<gid_t, 64> groups(size);
311311

312312
for (size_t i = 0; i < size; i++) {
313313
gid_t gid = gid_by_name(
314314
env->isolate(), groups_list->Get(env->context(), i).ToLocalChecked());
315315

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

326-
int rc = setgroups(size, groups);
327-
delete[] groups;
325+
int rc = setgroups(size, *groups);
328326

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

0 commit comments

Comments
 (0)