Merge branch 'jk/no-null-sha1-in-cache-tree'

Code to update the cache-tree has been tightened so that we won't
accidentally write out any 0{40} entry in the tree object.

* jk/no-null-sha1-in-cache-tree:
  cache-tree: reject entries with null sha1
maint
Junio C Hamano 2017-05-16 11:51:50 +09:00
commit f767178a5a
4 changed files with 35 additions and 2 deletions

View File

@ -354,7 +354,9 @@ static int update_one(struct cache_tree *it,
entlen = pathlen - baselen;
i++;
}
if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {

if (is_null_sha1(sha1) ||
(mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1))) {
strbuf_release(&buffer);
if (expected_missing)
return -1;

View File

@ -2197,6 +2197,7 @@ static int do_write_index(struct index_state *istate, int newfd,
int entries = istate->cache_nr;
struct stat st;
struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
int drop_cache_tree = 0;

for (i = removed = extended = 0; i < entries; i++) {
if (cache[i]->ce_flags & CE_REMOVE)
@ -2247,6 +2248,8 @@ static int do_write_index(struct index_state *istate, int newfd,
warning(msg, ce->name);
else
return error(msg, ce->name);

drop_cache_tree = 1;
}
if (ce_write_entry(&c, newfd, ce, previous_name) < 0)
return -1;
@ -2265,7 +2268,7 @@ static int do_write_index(struct index_state *istate, int newfd,
if (err)
return -1;
}
if (!strip_extensions && istate->cache_tree) {
if (!strip_extensions && !drop_cache_tree && istate->cache_tree) {
struct strbuf sb = STRBUF_INIT;

cache_tree_write(&sb, istate->cache_tree);

22
t/t1601-index-bogus.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/sh

test_description='test handling of bogus index entries'
. ./test-lib.sh

test_expect_success 'create tree with null sha1' '
tree=$(printf "160000 commit $_z40\\tbroken\\n" | git mktree)
'

test_expect_success 'read-tree refuses to read null sha1' '
test_must_fail git read-tree $tree
'

test_expect_success 'GIT_ALLOW_NULL_SHA1 overrides refusal' '
GIT_ALLOW_NULL_SHA1=1 git read-tree $tree
'

test_expect_success 'git write-tree refuses to write null sha1' '
test_must_fail git write-tree
'

test_done

View File

@ -31,6 +31,12 @@ test_expect_success 'setup: bring HEAD and index in sync' '
git commit -a -m "back to normal"
'

test_expect_success 'noop filter-branch complains' '
test_must_fail git filter-branch \
--force --prune-empty \
--index-filter "true"
'

test_expect_success 'filter commands are still checked' '
test_must_fail git filter-branch \
--force --prune-empty \