Commit Diff


commit - 5713faaf5800e9e90b79cb24ad3366c54a81db77
commit + 67fdb8a7a3ab501b2155b04b10e69e61779662b9
blob - f47d9defa60c0d149e6367abe262307f7fa4effe
blob + c0a98144883916b626903410a738267157a95ae4
--- got/got.c
+++ got/got.c
@@ -515,8 +515,13 @@ collect_import_msg(char **logmsg, char **logmsg_path, 
 
 	if (write(fd, initial_content, initial_content_len) == -1) {
 		err = got_error_from_errno2("write", *logmsg_path);
+		goto done;
+	}
+	if (close(fd) == -1) {
+		err = got_error_from_errno2("close", *logmsg_path);
 		goto done;
 	}
+	fd = -1;
 
 	err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
 	    initial_content_len, 1);
@@ -7404,8 +7409,13 @@ get_tag_message(char **tagmsg, char **tagmsg_path, con
 
 	if (write(fd, initial_content, initial_content_len) == -1) {
 		err = got_error_from_errno2("write", *tagmsg_path);
+		goto done;
+	}
+	if (close(fd) == -1) {
+		err = got_error_from_errno2("close", *tagmsg_path);
 		goto done;
 	}
+	fd = -1;
 
 	err = get_editor(&editor);
 	if (err)
@@ -8911,6 +8921,12 @@ collect_commit_logmsg(struct got_pathlist_head *commit
 		    diff_path);
 	}
 
+	if (close(fd) == -1) {
+		err = got_error_from_errno2("close", a->logmsg_path);
+		goto done;
+	}
+	fd = -1;
+
 	err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
 	    initial_content_len, a->prepared_log ? 0 : 1);
 done:
@@ -11660,7 +11676,7 @@ histedit_edit_logmsg(struct got_histedit_list_entry *h
 	const struct got_error *err = NULL;
 	struct got_commit_object *commit = NULL;
 	int logmsg_len;
-	int fd;
+	int fd = -1;
 	struct got_histedit_list_entry *folded = NULL;
 
 	err = got_object_open_as_commit(&commit, repo, hle->commit_id);
@@ -11709,8 +11725,15 @@ histedit_edit_logmsg(struct got_histedit_list_entry *h
 	if (err)
 		goto done;
 
-	write(fd, logmsg, logmsg_len);
-	close(fd);
+	if (write(fd, logmsg, logmsg_len) == -1) {
+		err = got_error_from_errno2("write", logmsg_path);
+		goto done;
+	}
+	if (close(fd) == -1) {
+		err = got_error_from_errno2("close", logmsg_path);
+		goto done;
+	}
+	fd = -1;
 
 	err = get_editor(&editor);
 	if (err)
@@ -11727,6 +11750,8 @@ histedit_edit_logmsg(struct got_histedit_list_entry *h
 			err = got_error_from_errno("strdup");
 	}
 done:
+	if (fd != -1 && close(fd) == -1 && err == NULL)
+		err = got_error_from_errno2("close", logmsg_path);
 	if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
 		err = got_error_from_errno2("unlink", logmsg_path);
 	free(logmsg_path);