mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:37:34 +00:00
LibC: Stop leaking FILE in getpwuid
and getpwnam
This commit is contained in:
parent
ee5ee0ef86
commit
b2454888e8
1 changed files with 3 additions and 0 deletions
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/ScopeGuard.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/TemporaryChange.h>
|
#include <AK/TemporaryChange.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
@ -51,6 +52,7 @@ void endpwent()
|
||||||
struct passwd* getpwuid(uid_t uid)
|
struct passwd* getpwuid(uid_t uid)
|
||||||
{
|
{
|
||||||
setpwent();
|
setpwent();
|
||||||
|
ScopeGuard guard = [] { endpwent(); };
|
||||||
while (auto* pw = getpwent()) {
|
while (auto* pw = getpwent()) {
|
||||||
if (pw->pw_uid == uid)
|
if (pw->pw_uid == uid)
|
||||||
return pw;
|
return pw;
|
||||||
|
@ -61,6 +63,7 @@ struct passwd* getpwuid(uid_t uid)
|
||||||
struct passwd* getpwnam(char const* name)
|
struct passwd* getpwnam(char const* name)
|
||||||
{
|
{
|
||||||
setpwent();
|
setpwent();
|
||||||
|
ScopeGuard guard = [] { endpwent(); };
|
||||||
while (auto* pw = getpwent()) {
|
while (auto* pw = getpwent()) {
|
||||||
if (!strcmp(pw->pw_name, name))
|
if (!strcmp(pw->pw_name, name))
|
||||||
return pw;
|
return pw;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue