[PATCH] mv: check symlink leading path on force overwrite
- From
- Dominik Maier <dmnk@google.com>
- Date
- Nov 19, 2026, 19:22 UTC
- Message-ID
- <20261119192252.2674835-1-dmnk@google.com>
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.
Signed-off-by: Dominik Maier <dmnk@google.com> --- builtin/mv.c | 3 ++- t/t7001-mv.sh | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/builtin/mv.c b/builtin/mv.c index 373d4aeba3..9c801e9cbb 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -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"); diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh index 719562a118..37815aadd1 100755 --- a/t/t7001-mv.sh +++ b/t/t7001-mv.sh @@ -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 &&
-- 2.56.1.750.gf70dc682cf-goog