]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use aprintf() instead of asprintf(3)
authorAlejandro Colomar <alx@kernel.org>
Wed, 1 Jan 2025 13:02:08 +0000 (14:02 +0100)
committerSerge Hallyn <serge@hallyn.com>
Wed, 4 Jun 2025 04:25:02 +0000 (23:25 -0500)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/commonio.c
lib/copydir.c
lib/groupio.c
lib/run_part.c
lib/tcbfuncs.c
src/useradd.c
src/userdel.c
src/vipw.c

index 38bf65c59dcf55c6479879188c8d1547a0986a84..7304ec006f61e6faf72365a887175b897954456f 100644 (file)
@@ -35,6 +35,7 @@
 #include "shadowlog_internal.h"
 #include "sssd.h"
 #include "string/memset/memzero.h"
+#include "string/sprintf/aprintf.h"
 #include "string/sprintf/snprintf.h"
 #include "string/strcmp/streq.h"
 #include "string/strcmp/strprefix.h"
@@ -361,9 +362,12 @@ int commonio_lock_nowait (struct commonio_db *db, bool log)
                return 1;
        }
 
-       if (asprintf(&file, "%s.%ju", db->filename, (uintmax_t) getpid()) == -1)
+       file = aprintf("%s.%ju", db->filename, (uintmax_t) getpid());
+       if (file == NULL)
                goto cleanup_ENOMEM;
-       if (asprintf(&lock, "%s.lock", db->filename) == -1)
+
+       lock = aprintf("%s.lock", db->filename);
+       if (lock == NULL)
                goto cleanup_ENOMEM;
 
        if (do_lock_file (file, lock, log) != 0) {
index 3a6c132b582bb95ab03cf2585bcec9b1a1cf0ef2..9dbe6fd6f44fd77c5a3ac55a38746b57791a8325 100644 (file)
@@ -38,6 +38,7 @@
 #include <attr/libattr.h>
 #endif                         /* WITH_ATTR */
 #include "shadowlog.h"
+#include "string/sprintf/aprintf.h"
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/streq.h"
 #include "string/strcmp/strprefix.h"
@@ -322,13 +323,13 @@ static int copy_tree_impl (const struct path_info *src, const struct path_info *
                        continue;
                }
 
-               if (asprintf(&src_name, "%s/%s", src->full_path, ent->d_name) == -1)
-               {
+               src_name = aprintf("%s/%s", src->full_path, ent->d_name);
+               if (src_name == NULL) {
                        err = -1;
                        continue;
                }
-               if (asprintf(&dst_name, "%s/%s", dst->full_path, ent->d_name) == -1)
-               {
+               dst_name = aprintf("%s/%s", dst->full_path, ent->d_name);
+               if (dst_name == NULL) {
                        err = -1;
                        goto skip;
                }
index a4348a1c731156d8795aee818b8d45d33e1bb955..72391433b139b58fe5605779913076223b47b45d 100644 (file)
@@ -23,6 +23,7 @@
 #include "getdef.h"
 #include "groupio.h"
 #include "prototypes.h"
+#include "string/sprintf/aprintf.h"
 #include "string/strcmp/streq.h"
 
 
@@ -325,7 +326,8 @@ static /*@null@*/struct commonio_entry *merge_group_entries (
        }
 
        /* Concatenate the 2 lines */
-       if (asprintf(&new_line, "%s\n%s", gr1->line, gr2->line) == -1)
+       new_line = aprintf("%s\n%s", gr1->line, gr2->line);
+       if (new_line == NULL)
                return NULL;
 
        /* Concatenate the 2 list of members */
index 890f837cd353ee9b98fc8cb9d46a7f9199d013e6..745dd354e4ce68268e28acf2e1a196a715a767ce 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "run_part.h"
 #include "shadowlog_internal.h"
+#include "string/sprintf/aprintf.h"
 
 
 static int run_part(char *script_path, const char *name, const char *action)
@@ -60,8 +61,9 @@ int run_parts(const char *directory, const char *name, const char *action)
                char         *s;
                struct stat  sb;
 
-               if (asprintf(&s, "%s/%s", directory, namelist[n]->d_name) == -1) {
-                       fprintf(shadow_logfd, "asprintf: %s\n", strerror(errno));
+               s = aprintf("%s/%s", directory, namelist[n]->d_name);
+               if (s == NULL) {
+                       fprintf(shadow_logfd, "aprintf: %s\n", strerror(errno));
                        for (; n<scanlist; n++) {
                                free(namelist[n]);
                        }
index 8ac1b563c66f34cd4527d343ec540a8c04306838..e40c5eeacd281dfa256c10d46efeabbaca85cc55 100644 (file)
@@ -24,6 +24,7 @@
 #include "tcbfuncs.h"
 #include "shadowio.h"
 #include "shadowlog_internal.h"
+#include "string/sprintf/aprintf.h"
 #include "string/strcmp/streq.h"
 
 
@@ -71,21 +72,23 @@ static /*@null@*/ char *shadowtcb_path_rel (const char *name, uid_t uid)
        char *ret;
 
        if (!getdef_bool ("TCB_SYMLINKS") || uid < SHADOWTCB_HASH_BY) {
-               if (asprintf (&ret, "%s", name) == -1) {
+               ret = aprintf("%s", name);
+               if (ret == NULL) {
                        OUT_OF_MEMORY;
                        return NULL;
                }
        } else if (uid < SHADOWTCB_HASH_BY * SHADOWTCB_HASH_BY) {
-               if (asprintf (&ret, ":%dK/%s",
-                             uid / SHADOWTCB_HASH_BY, name) == -1) {
+               ret = aprintf(":%dK/%s", uid / SHADOWTCB_HASH_BY, name);
+               if (ret == NULL) {
                        OUT_OF_MEMORY;
                        return NULL;
                }
        } else {
-               if (asprintf (&ret, ":%dM/:%dK/%s",
+               ret = aprintf(":%dM/:%dK/%s",
                              uid / (SHADOWTCB_HASH_BY * SHADOWTCB_HASH_BY),
                              (uid % (SHADOWTCB_HASH_BY * SHADOWTCB_HASH_BY)) / SHADOWTCB_HASH_BY,
-                       name) == -1) {
+                             name);
+               if (ret == NULL) {
                        OUT_OF_MEMORY;
                        return NULL;
                }
@@ -99,7 +102,8 @@ static /*@null@*/ char *shadowtcb_path_rel_existing (const char *name)
        struct stat st;
        char link[8192];
 
-       if (asprintf (&path, TCB_DIR "/%s", name) == -1) {
+       path = aprintf(TCB_DIR "/%s", name);
+       if (path == NULL) {
                OUT_OF_MEMORY;
                return NULL;
        }
@@ -150,7 +154,8 @@ static /*@null@*/ char *shadowtcb_path (const char *name, uid_t uid)
        if (NULL == rel) {
                return NULL;
        }
-       if (asprintf (&ret, TCB_DIR "/%s", rel) == -1) {
+       ret = aprintf(TCB_DIR "/%s", rel);
+       if (ret == NULL) {
                OUT_OF_MEMORY;
                free (rel);
                return NULL;
@@ -167,7 +172,8 @@ static /*@null@*/ char *shadowtcb_path_existing (const char *name)
        if (NULL == rel) {
                return NULL;
        }
-       if (asprintf (&ret, TCB_DIR "/%s", rel) == -1) {
+       ret = aprintf(TCB_DIR "/%s", rel);
+       if (ret == NULL) {
                OUT_OF_MEMORY;
                free (rel);
                return NULL;
@@ -193,7 +199,8 @@ static shadowtcb_status mkdir_leading (const char *name, uid_t uid)
        }
        while ((ind = strchr (ptr, '/'))) {
                stpcpy(ind, "");
-               if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
+               dir = aprintf(TCB_DIR "/%s", path);
+               if (dir == NULL) {
                        OUT_OF_MEMORY;
                        return SHADOWTCB_FAILURE;
                }
@@ -235,7 +242,8 @@ static shadowtcb_status unlink_suffs (const char *user)
        int i;
 
        for (i = 0; i < 3; i++) {
-               if (asprintf (&tmp, TCB_FMT "%s", user, suffs[i]) == -1) {
+               tmp = aprintf(TCB_FMT "%s", user, suffs[i]);
+               if (tmp == NULL) {
                        OUT_OF_MEMORY;
                        return SHADOWTCB_FAILURE;
                }
@@ -259,7 +267,8 @@ static shadowtcb_status rmdir_leading (char *path)
        shadowtcb_status ret = SHADOWTCB_SUCCESS;
        while ((ind = strrchr (path, '/'))) {
                stpcpy(ind, "");
-               if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
+               dir = aprintf(TCB_DIR "/%s", path);
+               if (dir == NULL) {
                        OUT_OF_MEMORY;
                        return SHADOWTCB_FAILURE;
                }
@@ -290,7 +299,8 @@ static shadowtcb_status move_dir (const char *user_newname, uid_t user_newid)
        if (NULL == stored_tcb_user) {
                return SHADOWTCB_FAILURE;
        }
-       if (asprintf (&olddir, TCB_DIR "/%s", stored_tcb_user) == -1) {
+       olddir = aprintf(TCB_DIR "/%s", stored_tcb_user);
+       if (olddir == NULL) {
                goto out_free_nomem;
        }
        if (stat (olddir, &oldmode) != 0) {
@@ -335,7 +345,8 @@ static shadowtcb_status move_dir (const char *user_newname, uid_t user_newid)
                         shadow_progname, olddir, strerror (errno));
                goto out_free;
        }
-       if (asprintf (&newdir, TCB_DIR "/%s", user_newname) == -1) {
+       newdir = aprintf(TCB_DIR "/%s", user_newname);
+       if (newdir == NULL) {
                goto out_free_nomem;
        }
        real_new_dir_rel = shadowtcb_path_rel (user_newname, the_newid);
@@ -379,7 +390,8 @@ shadowtcb_status shadowtcb_set_user (const char* name)
                OUT_OF_MEMORY;
                return SHADOWTCB_FAILURE;
        }
-       if (asprintf (&buf, TCB_FMT, name) == -1) {
+       buf = aprintf(TCB_FMT, name);
+       if (buf == NULL) {
                OUT_OF_MEMORY;
                return SHADOWTCB_FAILURE;
        }
@@ -403,7 +415,8 @@ shadowtcb_status shadowtcb_remove (const char *name)
        }
        free (path);
        free (rel);
-       if (asprintf (&path, TCB_DIR "/%s", name) == -1) {
+       path = aprintf(TCB_DIR "/%s", name);
+       if (path == NULL) {
                OUT_OF_MEMORY;
                return SHADOWTCB_FAILURE;
        }
@@ -435,11 +448,13 @@ shadowtcb_status shadowtcb_move (/*@NULL@*/const char *user_newname, uid_t user_
        if (-1 == user_newid) {
                return SHADOWTCB_SUCCESS;
        }
-       if (asprintf(&tcbdir, TCB_DIR "/%s", user_newname) == -1) {
+       tcbdir = aprintf(TCB_DIR "/%s", user_newname);
+       if (tcbdir == NULL) {
                OUT_OF_MEMORY;
                return SHADOWTCB_FAILURE;
        }
-       if (asprintf(&shadow, TCB_FMT, user_newname) == -1) {
+       shadow = aprintf(TCB_FMT, user_newname);
+       if (shadow == NULL) {
                OUT_OF_MEMORY;
                return SHADOWTCB_FAILURE;
        }
@@ -543,11 +558,13 @@ shadowtcb_status shadowtcb_create (const char *name, uid_t uid)
                }
        }
 
-       if (asprintf(&dir, TCB_DIR "/%s", name) == -1) {
+       dir = aprintf(TCB_DIR "/%s", name);
+       if (dir == NULL) {
                OUT_OF_MEMORY;
                return SHADOWTCB_FAILURE;
        }
-       if (asprintf(&shadow, TCB_FMT, name) == -1) {
+       shadow = aprintf(TCB_FMT, name);
+       if (shadow == NULL) {
                OUT_OF_MEMORY;
                return SHADOWTCB_FAILURE;
        }
index ed1e0bc566f5563f978ae58214851d0bc5a3eb54..0821e03ee3c80c628f5c655b5d88317d2b8c96e0 100644 (file)
@@ -66,6 +66,7 @@
 #include "shadowlog.h"
 #include "sssd.h"
 #include "string/memset/memzero.h"
+#include "string/sprintf/aprintf.h"
 #include "string/sprintf/snprintf.h"
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/strcaseeq.h"
@@ -330,7 +331,8 @@ get_defaults(void)
        const char  *ccp;
 
        if (prefix[0]) {
-               if (asprintf(&default_file, "%s/%s", prefix, USER_DEFAULTS_FILE) == -1)
+               default_file = aprintf("%s/%s", prefix, USER_DEFAULTS_FILE);
+               if (default_file == NULL)
                        return;
        }
 
@@ -531,16 +533,16 @@ set_defaults(void)
        FILE  *ofp;
 
 
-       if (asprintf(&new_file, "%s%s%s", prefix, prefix[0]?"/":"", NEW_USER_FILE) == -1)
-       {
+       new_file = aprintf("%s%s%s", prefix, prefix[0]?"/":"", NEW_USER_FILE);
+       if (new_file == NULL) {
                fprintf(stderr, _("%s: cannot create new defaults file: %s\n"),
                        Prog, strerror(errno));
                return -1;
         }
 
        if (prefix[0]) {
-               if (asprintf(&default_file, "%s/%s", prefix, USER_DEFAULTS_FILE) == -1)
-               {
+               default_file = aprintf("%s/%s", prefix, USER_DEFAULTS_FILE);
+               if (default_file == NULL) {
                        fprintf(stderr,
                                _("%s: cannot create new defaults file: %s\n"),
                                Prog, strerror(errno));
index 6bc4074bc41de7292d13648c4d5ca76a2e6c28b0..caf34606c582460253d6d5c95c89eaa23db49c06 100644 (file)
@@ -51,6 +51,7 @@
 #include "subordinateio.h"
 #endif                         /* ENABLE_SUBIDS */
 #include "shadowlog.h"
+#include "string/sprintf/aprintf.h"
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/streq.h"
 #include "string/strcmp/strprefix.h"
@@ -856,7 +857,8 @@ static int remove_tcbdir (const char *user_name, uid_t user_id)
                return 0;
        }
 
-       if (asprintf(&buf, TCB_DIR "/%s", user_name) == -1) {
+       buf = aprintf(TCB_DIR "/%s", user_name);
+       if (buf == NULL) {
                fprintf(stderr,
                        _("%s: Can't allocate memory, tcb entry for %s not removed.\n"),
                        Prog, user_name);
index dbc87db9aed9e0143880915debc81fc913e6df5a..e729d6dfb7fc0730d7ff83d685e6d01090d6105b 100644 (file)
@@ -43,6 +43,7 @@
 #endif                         /* WITH_TCB */
 #include "shadowlog.h"
 #include "sssd.h"
+#include "string/sprintf/aprintf.h"
 #include "string/sprintf/snprintf.h"
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/streq.h"
@@ -428,8 +429,9 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
                if (stat (file, &st1) != 0) {
                        vipwexit (_("failed to stat edited file"), errno, 1);
                }
-               if (asprintf(&to_rename, "%s+", file) == -1)
-                       vipwexit (_("asprintf(3) failed"), errno, 1);
+               to_rename = aprintf("%s+", file);
+               if (to_rename == NULL)
+                       vipwexit (_("aprintf() failed"), errno, 1);
 
                if (create_backup_file (f, to_rename, &st1) != 0) {
                        free(to_rename);