Skip to content

Commit

Permalink
LibC: Stop leaking FILE in getpwuid and getpwnam
Browse files Browse the repository at this point in the history
  • Loading branch information
timschumi authored and linusg committed Jul 6, 2022
1 parent ee5ee0e commit b245488
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Userland/Libraries/LibC/pwd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <AK/TemporaryChange.h>
#include <AK/Vector.h>
Expand Down Expand Up @@ -51,6 +52,7 @@ void endpwent()
struct passwd* getpwuid(uid_t uid)
{
setpwent();
ScopeGuard guard = [] { endpwent(); };
while (auto* pw = getpwent()) {
if (pw->pw_uid == uid)
return pw;
Expand All @@ -61,6 +63,7 @@ struct passwd* getpwuid(uid_t uid)
struct passwd* getpwnam(char const* name)
{
setpwent();
ScopeGuard guard = [] { endpwent(); };
while (auto* pw = getpwent()) {
if (!strcmp(pw->pw_name, name))
return pw;
Expand Down

0 comments on commit b245488

Please sign in to comment.