Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e015665

Browse files
committed
odb: git_odb_object contents are never NULL
This is a contract that we made in the library and that we need to uphold. The contents of a blob can never be NULL because several parts of the library (including the filter and attributes code) expect `git_blob_rawcontent` to always return a valid pointer.
1 parent fc6ac07 commit e015665

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/odb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,12 @@ static int hardcoded_objects(git_rawobj *raw, const git_oid *id)
762762
if (!git_oid_cmp(id, &empty_blob)) {
763763
raw->type = GIT_OBJ_BLOB;
764764
raw->len = 0;
765-
raw->data = NULL;
765+
raw->data = git__calloc(1, sizeof(uint8_t));
766766
return 0;
767767
} else if (!git_oid_cmp(id, &empty_tree)) {
768768
raw->type = GIT_OBJ_TREE;
769769
raw->len = 0;
770-
raw->data = NULL;
770+
raw->data = git__calloc(1, sizeof(uint8_t));
771771
return 0;
772772
} else {
773773
return GIT_ENOTFOUND;

tests/odb/emptyobjects.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ void test_odb_emptyobjects__read(void)
2121
cl_git_pass(git_oid_fromstr(&id, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
2222
cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
2323
cl_assert_equal_i(GIT_OBJ_BLOB, git_object_type((git_object *) blob));
24+
cl_assert(git_blob_rawcontent(blob));
25+
cl_assert_equal_s("", git_blob_rawcontent(blob));
2426
cl_assert_equal_i(0, git_blob_rawsize(blob));
2527
git_blob_free(blob);
2628
}
@@ -37,3 +39,19 @@ void test_odb_emptyobjects__read_tree(void)
3739
cl_assert_equal_p(NULL, git_tree_entry_byname(tree, "foo"));
3840
git_tree_free(tree);
3941
}
42+
43+
void test_odb_emptyobjects__read_tree_odb(void)
44+
{
45+
git_oid id;
46+
git_odb *odb;
47+
git_odb_object *tree_odb;
48+
49+
cl_git_pass(git_oid_fromstr(&id, "4b825dc642cb6eb9a060e54bf8d69288fbee4904"));
50+
cl_git_pass(git_repository_odb(&odb, g_repo));
51+
cl_git_pass(git_odb_read(&tree_odb, odb, &id));
52+
cl_assert(git_odb_object_data(tree_odb));
53+
cl_assert_equal_s("", git_odb_object_data(tree_odb));
54+
cl_assert_equal_i(0, git_odb_object_size(tree_odb));
55+
git_odb_object_free(tree_odb);
56+
git_odb_free(odb);
57+
}

0 commit comments

Comments
 (0)