From 1fb8408aa23c646c775e308a6cbbf27c43b0d79c Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Thu, 20 Jan 2022 21:07:19 +0100 Subject: [PATCH] LibCore: Do not leak FILE pointer in Group::add_group() By using a ScopeGuard we make sure that we always close the FILE, also on early returns. --- Userland/Libraries/LibCore/Group.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCore/Group.cpp b/Userland/Libraries/LibCore/Group.cpp index 585c32af09..eb4d098246 100644 --- a/Userland/Libraries/LibCore/Group.cpp +++ b/Userland/Libraries/LibCore/Group.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include @@ -48,11 +49,13 @@ ErrorOr Group::add_group(Group& group) if (!file) return Error::from_errno(errno); + ScopeGuard file_guard { [&] { + fclose(file); + } }; + if (putgrent(&gr, file) < 0) return Error::from_errno(errno); - fclose(file); - return {}; } #endif