From a7aa54eeeb07bb27f151e42dd91085256228e281 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Sun, 4 Jan 2026 16:28:39 +0100 Subject: [PATCH] setpwnam: only unlock when actually locked If no lock is held, do not unlock. With glibc this is fine, but let's not put too many expectations on library implementations. Signed-off-by: Tobias Stoeckmann --- login-utils/setpwnam.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/login-utils/setpwnam.c b/login-utils/setpwnam.c index d2ecb00ea..f95a7726d 100644 --- a/login-utils/setpwnam.c +++ b/login-utils/setpwnam.c @@ -75,7 +75,7 @@ int setpwnam(struct passwd *pwd, const char *prefix) { FILE *fp = NULL, *pwf = NULL; int save_errno, rc; - uint8_t found = 0; + uint8_t found = 0, locked = 0; size_t namelen; size_t contlen; size_t buflen = 256; @@ -94,6 +94,7 @@ int setpwnam(struct passwd *pwd, const char *prefix) /* acquire exclusive lock */ if (lckpwdf() < 0) goto fail; + locked = 1; pwf = fopen(PASSWD_FILE, "r"); if (!pwf) goto fail; @@ -174,7 +175,8 @@ int setpwnam(struct passwd *pwd, const char *prefix) fail: save_errno = errno; - ulckpwdf(); + if (locked) + ulckpwdf(); if (fp != NULL) fclose(fp); if (tmpname != NULL) -- 2.47.3