Commit Diff


commit - 50b0790ed9a28fced631f31e5b7ca76a9a610ea5
commit + 97972933210f224479bf2e8f44c4cb88a2dec393
blob - da2b1166ac33d3f36f0666ef6026f72a8c51cbcc
blob + 47edd6740e291c0126563b4b3e27ef5aed14614a
--- got/got.c
+++ got/got.c
@@ -489,7 +489,7 @@ collect_import_msg(char **logmsg, char **logmsg_path, 
 	char *initial_content = NULL;
 	const struct got_error *err = NULL;
 	int initial_content_len;
-	int fd;
+	int fd = -1;
 
 	initial_content_len = asprintf(&initial_content,
 	    "\n# %s to be imported to branch %s\n", path_dir,
@@ -502,11 +502,15 @@ collect_import_msg(char **logmsg, char **logmsg_path, 
 	if (err)
 		goto done;
 
-	write(fd, initial_content, initial_content_len);
-	close(fd);
+	if (write(fd, initial_content, initial_content_len) == -1) {
+		err = got_error_from_errno2("write", *logmsg_path);
+		goto done;
+	}
 
 	err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
 done:
+	if (fd != -1 && close(fd) == -1 && err == NULL)
+		err = got_error_from_errno2("close", *logmsg_path);
 	free(initial_content);
 	return err;
 }
@@ -5705,8 +5709,10 @@ get_tag_message(char **tagmsg, char **tagmsg_path, con
 	if (err)
 		goto done;
 
-	write(fd, initial_content, initial_content_len);
-	close(fd);
+	if (write(fd, initial_content, initial_content_len) == -1) {
+		err = got_error_from_errno2("write", *tagmsg_path);
+		goto done;
+	}
 
 	err = get_editor(&editor);
 	if (err)
@@ -5717,6 +5723,9 @@ done:
 	free(template);
 	free(editor);
 
+	if (fd != -1 && close(fd) == -1 && err == NULL)
+		err = got_error_from_errno2("close", *tagmsg_path);
+
 	/* Editor is done; we can now apply unveil(2) */
 	if (err == NULL) {
 		err = apply_unveil(repo_path, 0, NULL);
@@ -6551,7 +6560,7 @@ collect_commit_logmsg(struct got_pathlist_head *commit
 	char *template = NULL;
 	struct collect_commit_logmsg_arg *a = arg;
 	int initial_content_len;
-	int fd;
+	int fd = -1;
 	size_t len;
 
 	/* if a message was specified on the command line, just use it */
@@ -6577,7 +6586,10 @@ collect_commit_logmsg(struct got_pathlist_head *commit
 	if (err)
 		goto done;
 
-	write(fd, initial_content, initial_content_len);
+	if (write(fd, initial_content, initial_content_len) == -1) {
+		err = got_error_from_errno2("write", a->logmsg_path);
+		goto done;
+	}
 
 	TAILQ_FOREACH(pe, commitable_paths, entry) {
 		struct got_commitable *ct = pe->data;
@@ -6585,13 +6597,15 @@ collect_commit_logmsg(struct got_pathlist_head *commit
 		    got_commitable_get_status(ct),
 		    got_commitable_get_path(ct));
 	}
-	close(fd);
 
 	err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
 done:
 	free(initial_content);
 	free(template);
 
+	if (fd != -1 && close(fd) == -1 && err == NULL)
+		err = got_error_from_errno2("close", a->logmsg_path);
+
 	/* Editor is done; we can now apply unveil(2) */
 	if (err == NULL) {
 		err = apply_unveil(a->repo_path, 0, a->worktree_path);