From 6b4e7d0f789cfb9c654ee450df0fa8a3fecfb5b4 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 29 May 2020 13:51:00 +0200 Subject: [PATCH] tweaks: reshuffle a declaration and six calls of free(), to avoid a leak If the user chose to skip making a backup, 'backupname' would not be freed. --- src/files.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/files.c b/src/files.c index 6ddead29..e6705710 100644 --- a/src/files.c +++ b/src/files.c @@ -1552,6 +1552,8 @@ bool write_file(const char *name, FILE *thefile, bool tmp, /* Becomes TRUE when the file is non-temporary and exists. */ struct stat st; /* The status fields filled in by stat(). */ + char *backupname = NULL; + /* The name of the backup file, in case we make one. */ #endif char *realname = real_dir_from_tilde(name); /* The filename after tilde expansion. */ @@ -1589,7 +1591,6 @@ bool write_file(const char *name, FILE *thefile, bool tmp, (openfile->statinfo->st_mtime == st.st_mtime || method != OVERWRITE || openfile->mark)) { static struct timespec filetime[2]; - char *backupname; int backup_cflags, backup_fd, verdict; FILE *original = NULL, *backup_file = NULL; @@ -1638,7 +1639,6 @@ bool write_file(const char *name, FILE *thefile, bool tmp, * be fond of backups. Thus, without one, do not go on. */ if (*backupname == '\0') { statusline(ALERT, _("Too many existing backup files")); - free(backupname); goto cleanup_and_exit; } } @@ -1650,7 +1650,6 @@ bool write_file(const char *name, FILE *thefile, bool tmp, goto skip_backup; statusline(HUSH, _("Cannot delete backup %s: %s"), backupname, strerror(errno)); - free(backupname); goto cleanup_and_exit; } @@ -1671,7 +1670,6 @@ bool write_file(const char *name, FILE *thefile, bool tmp, goto skip_backup; statusline(HUSH, _("Cannot create backup %s: %s"), backupname, strerror(errno)); - free(backupname); goto cleanup_and_exit; } @@ -1699,7 +1697,6 @@ bool write_file(const char *name, FILE *thefile, bool tmp, goto skip_backup; statusline(HUSH, _("Cannot write backup %s: %s"), backupname, strerror(errno)); - free(backupname); goto cleanup_and_exit; } @@ -1713,11 +1710,8 @@ bool write_file(const char *name, FILE *thefile, bool tmp, goto skip_backup; statusline(HUSH, _("Cannot write backup %s: %s"), backupname, strerror(errno)); - free(backupname); goto cleanup_and_exit; } - - free(backupname); } skip_backup: @@ -1930,6 +1924,9 @@ bool write_file(const char *name, FILE *thefile, bool tmp, retval = TRUE; cleanup_and_exit: +#ifndef NANO_TINY + free(backupname); +#endif free(tempname); free(realname);