Skip to content

Commit c0487bd

Browse files
committed
tree: reject writing null-OID entries to a tree
In commit a96d3cc3f (cache-tree: reject entries with null sha1, 2017-04-21), the git.git project has changed its stance on null OIDs in tree objects. Previously, null OIDs were accepted in tree entries to help tools repair broken history. This resulted in some problems though in that many code paths mistakenly passed null OIDs to be added to a tree, which was not properly detected. Align our own code base according to the upstream change and reject writing tree entries early when the OID is all-zero.
1 parent 71c4306 commit c0487bd

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/tree.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ static int append_entry(
498498
if (!valid_entry_name(bld->repo, filename))
499499
return tree_error("failed to insert entry: invalid name for a tree entry", filename);
500500

501+
if (git_oid_iszero(id))
502+
return tree_error("failed to insert entry: invalid null OID for a tree entry", filename);
503+
501504
entry = alloc_entry(filename, strlen(filename), id);
502505
GITERR_CHECK_ALLOC(entry);
503506

@@ -740,6 +743,9 @@ int git_treebuilder_insert(
740743
if (!valid_entry_name(bld->repo, filename))
741744
return tree_error("failed to insert entry: invalid name for a tree entry", filename);
742745

746+
if (git_oid_iszero(id))
747+
return tree_error("failed to insert entry: invalid null OID", filename);
748+
743749
if (filemode != GIT_FILEMODE_COMMIT &&
744750
!git_object__is_valid(bld->repo, id, otype_from_mode(filemode)))
745751
return tree_error("failed to insert entry: invalid object specified", filename);

tests/object/tree/write.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,14 @@ void test_object_tree_write__object_validity(void)
512512
test_inserting_submodule();
513513
}
514514

515+
void test_object_tree_write__invalid_null_oid(void)
516+
{
517+
git_treebuilder *bld;
518+
git_oid null_oid = {{0}};
519+
520+
cl_git_pass(git_treebuilder_new(&bld, g_repo, NULL));
521+
cl_git_fail(git_treebuilder_insert(NULL, bld, "null_oid_file", &null_oid, GIT_FILEMODE_BLOB));
522+
cl_assert(giterr_last() && strstr(giterr_last()->message, "null OID") != NULL);
523+
524+
git_treebuilder_free(bld);
525+
}

0 commit comments

Comments
 (0)