Skip to content

Commit

Permalink
Merge branch 'ab/pack-stdin-packs-fix' into seen
Browse files Browse the repository at this point in the history
Input validation of "git pack-objects --stdin-packs" has been
corrected.

cf. <YND3h2l10PlnSNGJ@nand.local>

* ab/pack-stdin-packs-fix:
  pack-objects: fix segfault in --stdin-packs option
  pack-objects tests: cover blindspots in stdin handling
  • Loading branch information
gitster committed Jul 1, 2021
2 parents 85bc4c4 + be525e4 commit 9710351
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
10 changes: 10 additions & 0 deletions builtin/pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -3310,6 +3310,16 @@ static void read_packs_list_from_stdin(void)
item->util = p;
}

/*
* Arguments we got on stdin may not even be packs. Check that
* to avoid segfaulting later on in e.g. pack_mtime_cmp().
*/
for_each_string_list_item(item, &include_packs) {
struct packed_git *p = item->util;
if (!p)
die(_("could not find pack '%s'"), item->string);
}

/*
* First handle all of the excluded packs, marking them as kept in-core
* so that later calls to add_object_entry() discards any objects that
Expand Down
103 changes: 103 additions & 0 deletions t/t5300-pack-object.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,109 @@ test_expect_success 'setup' '
} >expect
'

test_expect_success 'setup pack-object <stdin' '
git init pack-object-stdin &&
test_commit -C pack-object-stdin one &&
test_commit -C pack-object-stdin two
'

test_expect_success 'pack-object <stdin parsing: basic [|--revs]' '
cat >in <<-EOF &&
$(git -C pack-object-stdin rev-parse one)
EOF
git -C pack-object-stdin pack-objects basic-stdin <in &&
idx=$(echo pack-object-stdin/basic-stdin-*.idx) &&
git show-index <"$idx" >actual &&
test_line_count = 1 actual &&
git -C pack-object-stdin pack-objects --revs basic-stdin-revs <in &&
idx=$(echo pack-object-stdin/basic-stdin-revs-*.idx) &&
git show-index <"$idx" >actual &&
test_line_count = 3 actual
'

test_expect_success 'pack-object <stdin parsing: [|--revs] bad line' '
cat >in <<-EOF &&
$(git -C pack-object-stdin rev-parse one)
garbage
$(git -C pack-object-stdin rev-parse two)
EOF
sed "s/^> //g" >err.expect <<-EOF &&
fatal: expected object ID, got garbage:
> garbage
EOF
test_must_fail git -C pack-object-stdin pack-objects bad-line-stdin <in 2>err.actual &&
test_cmp err.expect err.actual &&
cat >err.expect <<-EOF &&
fatal: bad revision '"'"'garbage'"'"'
EOF
test_must_fail git -C pack-object-stdin pack-objects --revs bad-line-stdin-revs <in 2>err.actual &&
test_cmp err.expect err.actual
'

test_expect_success 'pack-object <stdin parsing: [|--revs] empty line' '
cat >in <<-EOF &&
$(git -C pack-object-stdin rev-parse one)
$(git -C pack-object-stdin rev-parse two)
EOF
sed -e "s/^> //g" -e "s/Z$//g" >err.expect <<-EOF &&
fatal: expected object ID, got garbage:
> Z
EOF
test_must_fail git -C pack-object-stdin pack-objects empty-line-stdin <in 2>err.actual &&
test_cmp err.expect err.actual &&
git -C pack-object-stdin pack-objects --revs empty-line-stdin-revs <in &&
idx=$(echo pack-object-stdin/empty-line-stdin-revs-*.idx) &&
git show-index <"$idx" >actual &&
test_line_count = 3 actual
'

test_expect_success 'pack-object <stdin parsing: [|--revs] with --stdin' '
cat >in <<-EOF &&
$(git -C pack-object-stdin rev-parse one)
$(git -C pack-object-stdin rev-parse two)
EOF
# There is the "--stdin-packs is incompatible with --revs"
# test below, but we should make sure that the revision.c
# --stdin is not picked up
cat >err.expect <<-EOF &&
fatal: disallowed abbreviated or ambiguous option '"'"'stdin'"'"'
EOF
test_must_fail git -C pack-object-stdin pack-objects stdin-with-stdin-option --stdin <in 2>err.actual &&
test_cmp err.expect err.actual &&
test_must_fail git -C pack-object-stdin pack-objects --stdin --revs stdin-with-stdin-option-revs 2>err.actual <in &&
test_cmp err.expect err.actual
'

test_expect_success 'pack-object <stdin parsing: --stdin-packs handles garbage' '
cat >in <<-EOF &&
$(git -C pack-object-stdin rev-parse one)
$(git -C pack-object-stdin rev-parse two)
EOF
# We actually just report the first bad line in strcmp()
# order, it just so happens that we get the same result under
# SHA-1 and SHA-256 here. It does not really matter that we
# report the first bad item in this obscure case, so this
# oddity of the test is OK.
cat >err.expect <<-EOF &&
fatal: could not find pack '"'"'$(git -C pack-object-stdin rev-parse two)'"'"'
EOF
test_must_fail git -C pack-object-stdin pack-objects stdin-with-stdin-option --stdin-packs <in 2>err.actual &&
test_cmp err.expect err.actual
'

# usage: check_deltas <stderr_from_pack_objects> <cmp_op> <nr_deltas>
# e.g.: check_deltas stderr -gt 0
check_deltas() {
Expand Down

0 comments on commit 9710351

Please sign in to comment.