From 2e375aad04d047e12468f93300ad7e42a8a03ff3 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Fri, 15 Dec 2023 10:14:11 +0100 Subject: treewide: use asprintf to construct strings The asprintf function is considered as given for current code already. Use it instead of calling malloc + strcpy + strcat manually. Reported-by: Benny Baumann Signed-off-by: Tobias Stoeckmann --- modules/pam_faillock/faillock.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'modules/pam_faillock') diff --git a/modules/pam_faillock/faillock.c b/modules/pam_faillock/faillock.c index 091f253a..1d2057e7 100644 --- a/modules/pam_faillock/faillock.c +++ b/modules/pam_faillock/faillock.c @@ -36,6 +36,7 @@ #include "config.h" #include +#include #include #include #include @@ -55,23 +56,20 @@ open_tally (const char *dir, const char *user, uid_t uid, int create) { char *path; int flags = O_RDWR; - int fd; + int fd, r; if (dir == NULL || strstr(user, "../") != NULL) /* just a defensive programming as the user must be a * valid user on the system anyway */ return -1; - path = malloc(strlen(dir) + strlen(user) + 2); - if (path == NULL) + if (*dir && dir[strlen(dir) - 1] != '/') + r = asprintf(&path, "%s/%s", dir, user); + else + r = asprintf(&path, "%s%s", dir, user); + if (r < 0) return -1; - strcpy(path, dir); - if (*dir && dir[strlen(dir) - 1] != '/') { - strcat(path, "/"); - } - strcat(path, user); - if (create) { flags |= O_CREAT; if (access(dir, F_OK) != 0) { -- cgit v1.2.3