Skip to content

Commit 93cff9a

Browse files
peffgitster
authored andcommitted
sha1_loose_object_info: return error for corrupted objects
When sha1_loose_object_info() finds that a loose object file cannot be stat(2)ed or mmap(2)ed, it returns -1 to signal an error to the caller. However, if it found that the loose object file is corrupt and the object data cannot be used from it, it stuffs OBJ_BAD into "type" field of the object_info, but returns zero (i.e., success), which can confuse callers. This is due to 052fe5e (sha1_loose_object_info: make type lookup optional, 2013-07-12), which switched the return to a strict success/error, rather than returning the type (but botched the return). Callers of regular sha1_object_info() don't notice the difference, as that function returns the type (which is OBJ_BAD in this case). However, direct callers of sha1_object_info_extended() see the function return success, but without setting any meaningful values in the object_info struct, leading them to access potentially uninitialized memory. The easiest way to see the bug is via "cat-file -s", which will happily ignore the corruption and report whatever value happened to be in the "size" variable. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 49800c9 commit 93cff9a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

sha1_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2867,7 +2867,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,
28672867
if (status && oi->typep)
28682868
*oi->typep = status;
28692869
strbuf_release(&hdrbuf);
2870-
return 0;
2870+
return (status < 0) ? status : 0;
28712871
}
28722872

28732873
int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, unsigned flags)

t/t1060-object-corruption.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ test_expect_success 'streaming a corrupt blob fails' '
5353
)
5454
'
5555

56+
test_expect_success 'getting type of a corrupt blob fails' '
57+
(
58+
cd bit-error &&
59+
test_must_fail git cat-file -s HEAD:content.t
60+
)
61+
'
62+
5663
test_expect_success 'read-tree -u detects bit-errors in blobs' '
5764
(
5865
cd bit-error &&

0 commit comments

Comments
 (0)