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.
master
Benno Schulenberg 2020-05-29 13:51:00 +02:00
parent 4b4a8be58b
commit 6b4e7d0f78
1 changed files with 5 additions and 8 deletions

View File

@ -1552,6 +1552,8 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
/* Becomes TRUE when the file is non-temporary and exists. */ /* Becomes TRUE when the file is non-temporary and exists. */
struct stat st; struct stat st;
/* The status fields filled in by stat(). */ /* The status fields filled in by stat(). */
char *backupname = NULL;
/* The name of the backup file, in case we make one. */
#endif #endif
char *realname = real_dir_from_tilde(name); char *realname = real_dir_from_tilde(name);
/* The filename after tilde expansion. */ /* 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 || (openfile->statinfo->st_mtime == st.st_mtime ||
method != OVERWRITE || openfile->mark)) { method != OVERWRITE || openfile->mark)) {
static struct timespec filetime[2]; static struct timespec filetime[2];
char *backupname;
int backup_cflags, backup_fd, verdict; int backup_cflags, backup_fd, verdict;
FILE *original = NULL, *backup_file = NULL; 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. */ * be fond of backups. Thus, without one, do not go on. */
if (*backupname == '\0') { if (*backupname == '\0') {
statusline(ALERT, _("Too many existing backup files")); statusline(ALERT, _("Too many existing backup files"));
free(backupname);
goto cleanup_and_exit; goto cleanup_and_exit;
} }
} }
@ -1650,7 +1650,6 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
goto skip_backup; goto skip_backup;
statusline(HUSH, _("Cannot delete backup %s: %s"), statusline(HUSH, _("Cannot delete backup %s: %s"),
backupname, strerror(errno)); backupname, strerror(errno));
free(backupname);
goto cleanup_and_exit; goto cleanup_and_exit;
} }
@ -1671,7 +1670,6 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
goto skip_backup; goto skip_backup;
statusline(HUSH, _("Cannot create backup %s: %s"), statusline(HUSH, _("Cannot create backup %s: %s"),
backupname, strerror(errno)); backupname, strerror(errno));
free(backupname);
goto cleanup_and_exit; goto cleanup_and_exit;
} }
@ -1699,7 +1697,6 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
goto skip_backup; goto skip_backup;
statusline(HUSH, _("Cannot write backup %s: %s"), statusline(HUSH, _("Cannot write backup %s: %s"),
backupname, strerror(errno)); backupname, strerror(errno));
free(backupname);
goto cleanup_and_exit; goto cleanup_and_exit;
} }
@ -1713,11 +1710,8 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
goto skip_backup; goto skip_backup;
statusline(HUSH, _("Cannot write backup %s: %s"), statusline(HUSH, _("Cannot write backup %s: %s"),
backupname, strerror(errno)); backupname, strerror(errno));
free(backupname);
goto cleanup_and_exit; goto cleanup_and_exit;
} }
free(backupname);
} }
skip_backup: skip_backup:
@ -1930,6 +1924,9 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
retval = TRUE; retval = TRUE;
cleanup_and_exit: cleanup_and_exit:
#ifndef NANO_TINY
free(backupname);
#endif
free(tempname); free(tempname);
free(realname); free(realname);