summaryrefslogtreecommitdiff
path: root/notes-merge.c
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2016-07-07 22:08:30 +0200
committerJunio C Hamano <gitster@pobox.com>2016-07-07 14:16:26 -0700
commitdeb9c1575c456b9d12ff05fdd2bec516dfb34ae4 (patch)
tree5e4176e8274882e8867a465b6c822f214d15f2fa /notes-merge.c
parent0b65a8dbdb38962e700ee16776a3042beb489060 (diff)
downloadgit-deb9c1575c456b9d12ff05fdd2bec516dfb34ae4.tar.gz
notes-merge: use O_EXCL to avoid overwriting existing filesrs/notes-merge-no-toctou
Use the open(2) flag O_EXCL to ensure the file doesn't already exist instead of (racily) calling stat(2) through file_exists(). While at it switch to xopen() to reduce code duplication and get more consistent error messages. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'notes-merge.c')
-rw-r--r--notes-merge.c6
1 files changed, 1 insertions, 5 deletions
diff --git a/notes-merge.c b/notes-merge.c
index 34bfac0c68..f00059520e 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -298,12 +298,8 @@ static void write_buf_to_worktree(const unsigned char *obj,
char *path = git_pathdup(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
if (safe_create_leading_directories_const(path))
die_errno("unable to create directory for '%s'", path);
- if (file_exists(path))
- die("found existing file at '%s'", path);
- fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666);
- if (fd < 0)
- die_errno("failed to open '%s'", path);
+ fd = xopen(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
while (size > 0) {
long ret = write_in_full(fd, buf, size);