]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
setpwnam: only unlock when actually locked
authorTobias Stoeckmann <tobias@stoeckmann.org>
Sun, 4 Jan 2026 15:28:39 +0000 (16:28 +0100)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Sun, 4 Jan 2026 15:28:39 +0000 (16:28 +0100)
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 <tobias@stoeckmann.org>
login-utils/setpwnam.c

index d2ecb00ea33331e85ae9d05b27a05101216658a5..f95a7726d24126907ead5701ebe811c8f5124ba1 100644 (file)
@@ -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)