Skip to content

Commit 86303b1

Browse files
alejandro-colomarhallyn
authored andcommitted
lib/, src/: Use aprintf() instead of asprintf(3)
Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 39f41a6 commit 86303b1

File tree

8 files changed

+68
-36
lines changed

8 files changed

+68
-36
lines changed

lib/commonio.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "shadowlog_internal.h"
3636
#include "sssd.h"
3737
#include "string/memset/memzero.h"
38+
#include "string/sprintf/aprintf.h"
3839
#include "string/sprintf/snprintf.h"
3940
#include "string/strcmp/streq.h"
4041
#include "string/strcmp/strprefix.h"
@@ -361,9 +362,12 @@ int commonio_lock_nowait (struct commonio_db *db, bool log)
361362
return 1;
362363
}
363364

364-
if (asprintf(&file, "%s.%ju", db->filename, (uintmax_t) getpid()) == -1)
365+
file = aprintf("%s.%ju", db->filename, (uintmax_t) getpid());
366+
if (file == NULL)
365367
goto cleanup_ENOMEM;
366-
if (asprintf(&lock, "%s.lock", db->filename) == -1)
368+
369+
lock = aprintf("%s.lock", db->filename);
370+
if (lock == NULL)
367371
goto cleanup_ENOMEM;
368372

369373
if (do_lock_file (file, lock, log) != 0) {

lib/copydir.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <attr/libattr.h>
3939
#endif /* WITH_ATTR */
4040
#include "shadowlog.h"
41+
#include "string/sprintf/aprintf.h"
4142
#include "string/sprintf/xasprintf.h"
4243
#include "string/strcmp/streq.h"
4344
#include "string/strcmp/strprefix.h"
@@ -322,13 +323,13 @@ static int copy_tree_impl (const struct path_info *src, const struct path_info *
322323
continue;
323324
}
324325

325-
if (asprintf(&src_name, "%s/%s", src->full_path, ent->d_name) == -1)
326-
{
326+
src_name = aprintf("%s/%s", src->full_path, ent->d_name);
327+
if (src_name == NULL) {
327328
err = -1;
328329
continue;
329330
}
330-
if (asprintf(&dst_name, "%s/%s", dst->full_path, ent->d_name) == -1)
331-
{
331+
dst_name = aprintf("%s/%s", dst->full_path, ent->d_name);
332+
if (dst_name == NULL) {
332333
err = -1;
333334
goto skip;
334335
}

lib/groupio.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "getdef.h"
2424
#include "groupio.h"
2525
#include "prototypes.h"
26+
#include "string/sprintf/aprintf.h"
2627
#include "string/strcmp/streq.h"
2728

2829

@@ -325,7 +326,8 @@ static /*@null@*/struct commonio_entry *merge_group_entries (
325326
}
326327

327328
/* Concatenate the 2 lines */
328-
if (asprintf(&new_line, "%s\n%s", gr1->line, gr2->line) == -1)
329+
new_line = aprintf("%s\n%s", gr1->line, gr2->line);
330+
if (new_line == NULL)
329331
return NULL;
330332

331333
/* Concatenate the 2 list of members */

lib/run_part.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "run_part.h"
1515
#include "shadowlog_internal.h"
16+
#include "string/sprintf/aprintf.h"
1617

1718

1819
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)
6061
char *s;
6162
struct stat sb;
6263

63-
if (asprintf(&s, "%s/%s", directory, namelist[n]->d_name) == -1) {
64-
fprintf(shadow_logfd, "asprintf: %s\n", strerror(errno));
64+
s = aprintf("%s/%s", directory, namelist[n]->d_name);
65+
if (s == NULL) {
66+
fprintf(shadow_logfd, "aprintf: %s\n", strerror(errno));
6567
for (; n<scanlist; n++) {
6668
free(namelist[n]);
6769
}

lib/tcbfuncs.c

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "tcbfuncs.h"
2525
#include "shadowio.h"
2626
#include "shadowlog_internal.h"
27+
#include "string/sprintf/aprintf.h"
2728
#include "string/strcmp/streq.h"
2829

2930

@@ -71,21 +72,23 @@ static /*@null@*/ char *shadowtcb_path_rel (const char *name, uid_t uid)
7172
char *ret;
7273

7374
if (!getdef_bool ("TCB_SYMLINKS") || uid < SHADOWTCB_HASH_BY) {
74-
if (asprintf (&ret, "%s", name) == -1) {
75+
ret = aprintf("%s", name);
76+
if (ret == NULL) {
7577
OUT_OF_MEMORY;
7678
return NULL;
7779
}
7880
} else if (uid < SHADOWTCB_HASH_BY * SHADOWTCB_HASH_BY) {
79-
if (asprintf (&ret, ":%dK/%s",
80-
uid / SHADOWTCB_HASH_BY, name) == -1) {
81+
ret = aprintf(":%dK/%s", uid / SHADOWTCB_HASH_BY, name);
82+
if (ret == NULL) {
8183
OUT_OF_MEMORY;
8284
return NULL;
8385
}
8486
} else {
85-
if (asprintf (&ret, ":%dM/:%dK/%s",
87+
ret = aprintf(":%dM/:%dK/%s",
8688
uid / (SHADOWTCB_HASH_BY * SHADOWTCB_HASH_BY),
8789
(uid % (SHADOWTCB_HASH_BY * SHADOWTCB_HASH_BY)) / SHADOWTCB_HASH_BY,
88-
name) == -1) {
90+
name);
91+
if (ret == NULL) {
8992
OUT_OF_MEMORY;
9093
return NULL;
9194
}
@@ -99,7 +102,8 @@ static /*@null@*/ char *shadowtcb_path_rel_existing (const char *name)
99102
struct stat st;
100103
char link[8192];
101104

102-
if (asprintf (&path, TCB_DIR "/%s", name) == -1) {
105+
path = aprintf(TCB_DIR "/%s", name);
106+
if (path == NULL) {
103107
OUT_OF_MEMORY;
104108
return NULL;
105109
}
@@ -150,7 +154,8 @@ static /*@null@*/ char *shadowtcb_path (const char *name, uid_t uid)
150154
if (NULL == rel) {
151155
return NULL;
152156
}
153-
if (asprintf (&ret, TCB_DIR "/%s", rel) == -1) {
157+
ret = aprintf(TCB_DIR "/%s", rel);
158+
if (ret == NULL) {
154159
OUT_OF_MEMORY;
155160
free (rel);
156161
return NULL;
@@ -167,7 +172,8 @@ static /*@null@*/ char *shadowtcb_path_existing (const char *name)
167172
if (NULL == rel) {
168173
return NULL;
169174
}
170-
if (asprintf (&ret, TCB_DIR "/%s", rel) == -1) {
175+
ret = aprintf(TCB_DIR "/%s", rel);
176+
if (ret == NULL) {
171177
OUT_OF_MEMORY;
172178
free (rel);
173179
return NULL;
@@ -193,7 +199,8 @@ static shadowtcb_status mkdir_leading (const char *name, uid_t uid)
193199
}
194200
while ((ind = strchr (ptr, '/'))) {
195201
stpcpy(ind, "");
196-
if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
202+
dir = aprintf(TCB_DIR "/%s", path);
203+
if (dir == NULL) {
197204
OUT_OF_MEMORY;
198205
return SHADOWTCB_FAILURE;
199206
}
@@ -235,7 +242,8 @@ static shadowtcb_status unlink_suffs (const char *user)
235242
int i;
236243

237244
for (i = 0; i < 3; i++) {
238-
if (asprintf (&tmp, TCB_FMT "%s", user, suffs[i]) == -1) {
245+
tmp = aprintf(TCB_FMT "%s", user, suffs[i]);
246+
if (tmp == NULL) {
239247
OUT_OF_MEMORY;
240248
return SHADOWTCB_FAILURE;
241249
}
@@ -259,7 +267,8 @@ static shadowtcb_status rmdir_leading (char *path)
259267
shadowtcb_status ret = SHADOWTCB_SUCCESS;
260268
while ((ind = strrchr (path, '/'))) {
261269
stpcpy(ind, "");
262-
if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
270+
dir = aprintf(TCB_DIR "/%s", path);
271+
if (dir == NULL) {
263272
OUT_OF_MEMORY;
264273
return SHADOWTCB_FAILURE;
265274
}
@@ -290,7 +299,8 @@ static shadowtcb_status move_dir (const char *user_newname, uid_t user_newid)
290299
if (NULL == stored_tcb_user) {
291300
return SHADOWTCB_FAILURE;
292301
}
293-
if (asprintf (&olddir, TCB_DIR "/%s", stored_tcb_user) == -1) {
302+
olddir = aprintf(TCB_DIR "/%s", stored_tcb_user);
303+
if (olddir == NULL) {
294304
goto out_free_nomem;
295305
}
296306
if (stat (olddir, &oldmode) != 0) {
@@ -335,7 +345,8 @@ static shadowtcb_status move_dir (const char *user_newname, uid_t user_newid)
335345
shadow_progname, olddir, strerror (errno));
336346
goto out_free;
337347
}
338-
if (asprintf (&newdir, TCB_DIR "/%s", user_newname) == -1) {
348+
newdir = aprintf(TCB_DIR "/%s", user_newname);
349+
if (newdir == NULL) {
339350
goto out_free_nomem;
340351
}
341352
real_new_dir_rel = shadowtcb_path_rel (user_newname, the_newid);
@@ -379,7 +390,8 @@ shadowtcb_status shadowtcb_set_user (const char* name)
379390
OUT_OF_MEMORY;
380391
return SHADOWTCB_FAILURE;
381392
}
382-
if (asprintf (&buf, TCB_FMT, name) == -1) {
393+
buf = aprintf(TCB_FMT, name);
394+
if (buf == NULL) {
383395
OUT_OF_MEMORY;
384396
return SHADOWTCB_FAILURE;
385397
}
@@ -403,7 +415,8 @@ shadowtcb_status shadowtcb_remove (const char *name)
403415
}
404416
free (path);
405417
free (rel);
406-
if (asprintf (&path, TCB_DIR "/%s", name) == -1) {
418+
path = aprintf(TCB_DIR "/%s", name);
419+
if (path == NULL) {
407420
OUT_OF_MEMORY;
408421
return SHADOWTCB_FAILURE;
409422
}
@@ -435,11 +448,13 @@ shadowtcb_status shadowtcb_move (/*@NULL@*/const char *user_newname, uid_t user_
435448
if (-1 == user_newid) {
436449
return SHADOWTCB_SUCCESS;
437450
}
438-
if (asprintf(&tcbdir, TCB_DIR "/%s", user_newname) == -1) {
451+
tcbdir = aprintf(TCB_DIR "/%s", user_newname);
452+
if (tcbdir == NULL) {
439453
OUT_OF_MEMORY;
440454
return SHADOWTCB_FAILURE;
441455
}
442-
if (asprintf(&shadow, TCB_FMT, user_newname) == -1) {
456+
shadow = aprintf(TCB_FMT, user_newname);
457+
if (shadow == NULL) {
443458
OUT_OF_MEMORY;
444459
return SHADOWTCB_FAILURE;
445460
}
@@ -543,11 +558,13 @@ shadowtcb_status shadowtcb_create (const char *name, uid_t uid)
543558
}
544559
}
545560

546-
if (asprintf(&dir, TCB_DIR "/%s", name) == -1) {
561+
dir = aprintf(TCB_DIR "/%s", name);
562+
if (dir == NULL) {
547563
OUT_OF_MEMORY;
548564
return SHADOWTCB_FAILURE;
549565
}
550-
if (asprintf(&shadow, TCB_FMT, name) == -1) {
566+
shadow = aprintf(TCB_FMT, name);
567+
if (shadow == NULL) {
551568
OUT_OF_MEMORY;
552569
return SHADOWTCB_FAILURE;
553570
}

src/useradd.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "shadowlog.h"
6767
#include "sssd.h"
6868
#include "string/memset/memzero.h"
69+
#include "string/sprintf/aprintf.h"
6970
#include "string/sprintf/snprintf.h"
7071
#include "string/sprintf/xasprintf.h"
7172
#include "string/strcmp/strcaseeq.h"
@@ -330,7 +331,8 @@ get_defaults(void)
330331
const char *ccp;
331332

332333
if (prefix[0]) {
333-
if (asprintf(&default_file, "%s/%s", prefix, USER_DEFAULTS_FILE) == -1)
334+
default_file = aprintf("%s/%s", prefix, USER_DEFAULTS_FILE);
335+
if (default_file == NULL)
334336
return;
335337
}
336338

@@ -531,16 +533,16 @@ set_defaults(void)
531533
FILE *ofp;
532534

533535

534-
if (asprintf(&new_file, "%s%s%s", prefix, prefix[0]?"/":"", NEW_USER_FILE) == -1)
535-
{
536+
new_file = aprintf("%s%s%s", prefix, prefix[0]?"/":"", NEW_USER_FILE);
537+
if (new_file == NULL) {
536538
fprintf(stderr, _("%s: cannot create new defaults file: %s\n"),
537539
Prog, strerror(errno));
538540
return -1;
539541
}
540542

541543
if (prefix[0]) {
542-
if (asprintf(&default_file, "%s/%s", prefix, USER_DEFAULTS_FILE) == -1)
543-
{
544+
default_file = aprintf("%s/%s", prefix, USER_DEFAULTS_FILE);
545+
if (default_file == NULL) {
544546
fprintf(stderr,
545547
_("%s: cannot create new defaults file: %s\n"),
546548
Prog, strerror(errno));

src/userdel.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "subordinateio.h"
5252
#endif /* ENABLE_SUBIDS */
5353
#include "shadowlog.h"
54+
#include "string/sprintf/aprintf.h"
5455
#include "string/sprintf/xasprintf.h"
5556
#include "string/strcmp/streq.h"
5657
#include "string/strcmp/strprefix.h"
@@ -856,7 +857,8 @@ static int remove_tcbdir (const char *user_name, uid_t user_id)
856857
return 0;
857858
}
858859

859-
if (asprintf(&buf, TCB_DIR "/%s", user_name) == -1) {
860+
buf = aprintf(TCB_DIR "/%s", user_name);
861+
if (buf == NULL) {
860862
fprintf(stderr,
861863
_("%s: Can't allocate memory, tcb entry for %s not removed.\n"),
862864
Prog, user_name);

src/vipw.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#endif /* WITH_TCB */
4444
#include "shadowlog.h"
4545
#include "sssd.h"
46+
#include "string/sprintf/aprintf.h"
4647
#include "string/sprintf/snprintf.h"
4748
#include "string/sprintf/xasprintf.h"
4849
#include "string/strcmp/streq.h"
@@ -428,8 +429,9 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
428429
if (stat (file, &st1) != 0) {
429430
vipwexit (_("failed to stat edited file"), errno, 1);
430431
}
431-
if (asprintf(&to_rename, "%s+", file) == -1)
432-
vipwexit (_("asprintf(3) failed"), errno, 1);
432+
to_rename = aprintf("%s+", file);
433+
if (to_rename == NULL)
434+
vipwexit (_("aprintf() failed"), errno, 1);
433435

434436
if (create_backup_file (f, to_rename, &st1) != 0) {
435437
free(to_rename);

0 commit comments

Comments
 (0)