git/t/t3438-rebase-broken-files.sh
Jeff King 647e870a08 rebase: use child_process_clear() to clean
In the run_am() function, we set up a child_process struct to run
"git-am", allocating memory for its args and env strvecs. These are
normally cleaned up when we call run_command(). But if we encounter
certain errors, we exit the function early and try to clean up ourselves
by clearing the am.args field. This leaks the "env" strvec.

We should use child_process_clear() instead, which covers both. And more
importantly, it future proofs us against the struct ever growing more
allocated fields.

These are unlikely errors to happen in practice, so they don't actually
trigger the leak sanitizer in the tests. But we can add a new test which
does exercise one of the paths (and fails SANITIZE=leak without this
patch).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-22 10:21:35 -07:00

70 lines
1.5 KiB
Bash
Executable file

#!/bin/sh
test_description='rebase behavior when on-disk files are broken'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'set up conflicting branches' '
test_commit base file &&
git checkout -b branch1 &&
test_commit one file &&
git checkout -b branch2 HEAD^ &&
test_commit two file
'
create_conflict () {
test_when_finished "git rebase --abort" &&
git checkout -B tmp branch2 &&
test_must_fail git rebase branch1
}
check_resolve_fails () {
echo resolved >file &&
git add file &&
test_must_fail git rebase --continue
}
for item in NAME EMAIL DATE
do
test_expect_success "detect missing GIT_AUTHOR_$item" '
create_conflict &&
grep -v $item .git/rebase-merge/author-script >tmp &&
mv tmp .git/rebase-merge/author-script &&
check_resolve_fails
'
done
for item in NAME EMAIL DATE
do
test_expect_success "detect duplicate GIT_AUTHOR_$item" '
create_conflict &&
grep -i $item .git/rebase-merge/author-script >tmp &&
cat tmp >>.git/rebase-merge/author-script &&
check_resolve_fails
'
done
test_expect_success 'unknown key in author-script' '
create_conflict &&
echo "GIT_AUTHOR_BOGUS=${SQ}whatever${SQ}" \
>>.git/rebase-merge/author-script &&
check_resolve_fails
'
test_expect_success POSIXPERM,SANITY 'unwritable rebased-patches does not leak' '
>.git/rebased-patches &&
chmod a-w .git/rebased-patches &&
git checkout -b side HEAD^ &&
test_commit unrelated &&
test_must_fail git rebase --apply --onto tmp HEAD^
'
test_done