From 275da329517b7d72abf25ce6a9ada48b142c64da Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 12 Apr 2021 13:53:19 +0200 Subject: Fix memory leak in libcontacts_save_contact MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libcontacts_save_contact.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libcontacts_save_contact.c b/libcontacts_save_contact.c index 89f8ebc..b373a46 100644 --- a/libcontacts_save_contact.c +++ b/libcontacts_save_contact.c @@ -31,7 +31,7 @@ makedirs(char *path) int libcontacts_save_contact(struct libcontacts_contact *contact, const struct passwd *user) { - char *data = NULL, *path = NULL, *tmppath; + char *data = NULL, *path = NULL, *tmppath = NULL; int oflags = O_WRONLY | O_CREAT | O_TRUNC; int fd = -1, saved_errno = errno, dirs_created = 0; ssize_t r; @@ -74,7 +74,7 @@ libcontacts_save_contact(struct libcontacts_contact *contact, const struct passw if (!path) goto fail; - tmppath = alloca(strlen(path) + sizeof("~")); + tmppath = malloc(strlen(path) + sizeof("~")); stpcpy(stpcpy(tmppath, path), "~"); if (oflags & O_EXCL) { @@ -93,6 +93,8 @@ libcontacts_save_contact(struct libcontacts_contact *contact, const struct passw basenam = contact->id; contact->id = NULL; } + free(path); + free(tmppath); goto generate_id; } close(fd); @@ -132,6 +134,7 @@ open_again: free(data); free(path); + free(tmppath); free(basenam); errno = saved_errno; return 0; @@ -142,6 +145,7 @@ fail: close(fd); free(data); free(path); + free(tmppath); free(basenam); errno = saved_errno; return -1; -- cgit v1.2.3-70-g09d2