git/t/t3307-notes-man.sh
Ævar Arnfjörð Bjarmason 055e57b7b2 log: fix a memory leak in "git show <revision>..."
Fix a memory leak in code added in 5d7eeee2ac (git-show: grok blobs,
trees and tags, too, 2006-12-14). As we iterate over a "<revision>..."
command-line and encounter ad OBJ_COMMIT we want to use our "struct
rev_info", but with a "pending" array of one element: the one commit
we're showing in the loop.

To do this 5d7eeee2ac saved away a pointer to rev.pending.objects and
rev.pending.nr for its iteration. We'd then clobber those (and alloc)
when we needed to show an OBJ_COMMIT.

We'd therefore leak the "rev.pending" we started out with, and only
free the new "rev.pending" in the "OBJ_COMMIT" case arm as
prepare_revision_walk() would draw it down.

Let's fix this memory leak. Now when we encounter an OBJ_COMMIT we
save away the "rev.pending" before clearing it. We then add a single
commit to it, which our indirect invocation of prepare_revision_walk()
will remove. After that we restore the "rev.pending".

Our "rev.pending" will then get free'd by the release_revisions()
added in f6bfea0ad0 (revisions API users: use release_revisions() in
builtin/log.c, 2022-04-13)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-08-03 10:16:28 -07:00

40 lines
867 B
Bash
Executable File

#!/bin/sh
test_description='Examples from the git-notes man page
Make sure the manual is not full of lies.'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'setup' '
test_commit A &&
test_commit B &&
test_commit C
'
test_expect_success 'example 1: notes to add an Acked-by line' '
cat <<-\EOF >expect &&
B
Notes:
Acked-by: A C Ker <acker@example.com>
EOF
git notes add -m "Acked-by: A C Ker <acker@example.com>" B &&
git show -s B^{commit} >log &&
tail -n 4 log >actual &&
test_cmp expect actual
'
test_expect_success 'example 2: binary notes' '
cp "$TEST_DIRECTORY"/test-binary-1.png . &&
git checkout B &&
blob=$(git hash-object -w test-binary-1.png) &&
git notes --ref=logo add -C "$blob" &&
git notes --ref=logo copy B C &&
git notes --ref=logo show C >actual &&
test_cmp test-binary-1.png actual
'
test_done