Skip to contentSkip to conversation

mv: check symlink leading path on force overwrite

PATCH New Dominik Maier 1 participant 1 message Nov 19

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mv: check symlink leading path on force overwrite

Commit 062d90b2a8 (mv: reject a destination whose leading path is missing or a symlink, 2026-07-30) taught cmd_mv() to reject destinations whose leading path contains a symbolic link via has_symlink_leading_path(). Its "mv -f" test used a dangling symlink, so lstat(dst, &st) failed with ENOENT and fell through to the new check.

When the target file beyond the symlinked directory already exists on disk, lstat(dst, &st) succeeds, "-f" clears "bad = NULL", and the unconditional "goto act_on_entry" skips src_for_dst, has_symlink_leading_path(), and the sparse-checkout checks, overwriting the file through the symlink.

Only jump to act_on_entry when "bad" is non-NULL so forced overwrites still run the remaining destination checks.

---
 builtin/mv.c  |  3 ++-
 t/t7001-mv.sh | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
builtin/mv.c +2 -1
index 373d4aeba3..9c801e9cbb 100644
@@ -440,7 +440,8 @@ int cmd_mv(int argc,
 				} else
 					bad = _("Cannot overwrite");
 			}
-			goto act_on_entry;
+			if (bad)
+				goto act_on_entry;
 		}
 		if (string_list_has_string(&src_for_dst, dst)) {
 			bad = _("multiple sources for the same target");
t/t7001-mv.sh +14 -0
index 719562a118..37815aadd1 100755
@@ -207,6 +207,20 @@ test_expect_success SYMLINKS 'mv -f does not follow a symlinked leading path' '
 	test_grep "destination is beyond a symbolic link" actual
 '
 
+test_expect_success SYMLINKS 'mv -f does not overwrite existing target beyond a symlinked leading path' '
+	git reset --hard &&
+	rm -rf from target_dir &&
+	mkdir from target_dir &&
+	echo contents >from/src &&
+	echo keep >target_dir/dst &&
+	ln -s ../target_dir from/link &&
+	git add from/src from/link &&
+	test_must_fail git mv -f from/src from/link/dst 2>actual &&
+	test_grep "destination is beyond a symbolic link" actual &&
+	echo keep >expect &&
+	test_cmp expect target_dir/dst
+'
+
 test_expect_success 'mv --dry-run detects non-existent destination parent directory' '
 	git reset --hard &&
 	rm -rf from && mkdir from &&
Signature · 1 line
2.56.1.750.gf70dc682cf-goog