From de57d8b722d013f7ddfd25e5e0bf98ae146d7749 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@telfort.nl>
Date: Tue, 15 Aug 2017 13:46:20 +0200
Subject: [PATCH] files: don't try to open a negative file descriptor 
 [coverity scan]

---
 src/files.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/files.c b/src/files.c
index db942893..b37553b9 100644
--- a/src/files.c
+++ b/src/files.c
@@ -1604,7 +1604,7 @@ bool write_file(const char *name, FILE *f_open, bool tmp,
 		(method != OVERWRITE || openfile->mark_set ||
 		openfile->current_stat->st_mtime == st.st_mtime)) {
 	int backup_fd;
-	FILE *backup_file;
+	FILE *backup_file = NULL;
 	char *backupname;
 	static struct timespec filetime[2];
 	int backup_cflags;
@@ -1695,11 +1695,11 @@ bool write_file(const char *name, FILE *f_open, bool tmp,
 
 	backup_fd = open(backupname, backup_cflags,
 		S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
-	/* Now we've got a safe file stream.  If the previous open()
-	 * call failed, this will return NULL. */
-	backup_file = fdopen(backup_fd, "wb");
 
-	if (backup_fd < 0 || backup_file == NULL) {
+	if (backup_fd >= 0)
+	    backup_file = fdopen(backup_fd, "wb");
+
+	if (backup_file == NULL) {
 	    statusline(HUSH, _("Error writing backup file %s: %s"),
 			backupname, strerror(errno));
 	    free(backupname);
-- 
GitLab