Blame SOURCES/git-cve-2023-25652.patch

e80581
diff -up ./builtin/apply.c.original ./builtin/apply.c
e80581
--- ./builtin/apply.c.original	2023-05-18 14:44:01.287997256 +0900
e80581
+++ ./builtin/apply.c	2023-05-18 14:44:45.513756974 +0900
e80581
@@ -4151,7 +4151,7 @@ static int write_out_one_reject(struct p
e80581
 	FILE *rej;
e80581
 	char namebuf[PATH_MAX];
e80581
 	struct fragment *frag;
e80581
-	int cnt = 0;
e80581
+	int fd, cnt = 0;
e80581
 	struct strbuf sb = STRBUF_INIT;
e80581
 
e80581
 	for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
e80581
@@ -4190,7 +4190,17 @@ static int write_out_one_reject(struct p
e80581
 	memcpy(namebuf, patch->new_name, cnt);
e80581
 	memcpy(namebuf + cnt, ".rej", 5);
e80581
 
e80581
-	rej = fopen(namebuf, "w");
e80581
+	fd = open(namebuf, O_CREAT | O_EXCL | O_WRONLY, 0666);
e80581
+	if (fd < 0) {
e80581
+		if (errno != EEXIST)
e80581
+			die_errno(_("cannot open %s"), namebuf);
e80581
+		if (unlink(namebuf))
e80581
+			die_errno(_("cannot unlink '%s'"), namebuf);
e80581
+		fd = open(namebuf, O_CREAT | O_EXCL | O_WRONLY, 0666);
e80581
+		if (fd < 0)
e80581
+			die_errno(_("cannot open %s"), namebuf);
e80581
+	}
e80581
+	rej = fdopen(fd, "w");
e80581
 	if (!rej)
e80581
 		return error(_("cannot open %s: %s"), namebuf, strerror(errno));
e80581
 
e80581
diff -up ./t/t4115-apply-symlink.sh.original ./t/t4115-apply-symlink.sh
e80581
--- ./t/t4115-apply-symlink.sh.original	2023-05-18 14:45:50.675876356 +0900
e80581
+++ ./t/t4115-apply-symlink.sh	2023-05-18 14:46:42.075759315 +0900
e80581
@@ -46,4 +46,19 @@ test_expect_success SYMLINKS 'apply --in
e80581
 
e80581
 '
e80581
 
e80581
+test_expect_success SYMLINKS '--reject removes .rej symlink if it exists' '
e80581
+	test_when_finished "git reset --hard && git clean -dfx" &&
e80581
+
e80581
+	test_commit file &&
e80581
+	echo modified >file.t &&
e80581
+	git diff -- file.t >patch &&
e80581
+	echo modified-again >file.t &&
e80581
+
e80581
+	ln -s foo file.t.rej &&
e80581
+	test_must_fail git apply patch --reject 2>err &&
e80581
+	test_i18ngrep "Rejected hunk" err &&
e80581
+	test_path_is_missing foo &&
e80581
+	test_path_is_file file.t.rej
e80581
+'
e80581
+
e80581
 test_done