@@ -1362,7 +1362,7 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
13621362 obj_read_unlock ();
13631363 status = git_inflate (stream , 0 );
13641364 obj_read_lock ();
1365- if (status < Z_OK )
1365+ if (status != Z_OK && status != Z_STREAM_END )
13661366 return ULHR_BAD ;
13671367
13681368 /*
@@ -1385,20 +1385,19 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
13851385 * reading the stream.
13861386 */
13871387 strbuf_add (header , buffer , stream -> next_out - (unsigned char * )buffer );
1388- stream -> next_out = buffer ;
1389- stream -> avail_out = bufsiz ;
13901388
13911389 do {
1390+ stream -> next_out = buffer ;
1391+ stream -> avail_out = bufsiz ;
1392+
13921393 obj_read_unlock ();
13931394 status = git_inflate (stream , 0 );
13941395 obj_read_lock ();
13951396 strbuf_add (header , buffer , stream -> next_out - (unsigned char * )buffer );
13961397 if (memchr (buffer , '\0' , stream -> next_out - (unsigned char * )buffer ))
13971398 return 0 ;
1398- stream -> next_out = buffer ;
1399- stream -> avail_out = bufsiz ;
1400- } while (status != Z_STREAM_END );
1401- return ULHR_TOO_LONG ;
1399+ } while (status == Z_OK );
1400+ return ULHR_BAD ;
14021401}
14031402
14041403static void * unpack_loose_rest (git_zstream * stream ,
@@ -1437,18 +1436,17 @@ static void *unpack_loose_rest(git_zstream *stream,
14371436 obj_read_lock ();
14381437 }
14391438 }
1440- if (status == Z_STREAM_END && !stream -> avail_in ) {
1441- git_inflate_end (stream );
1442- return buf ;
1443- }
14441439
1445- if (status < 0 )
1440+ if (status != Z_STREAM_END ) {
14461441 error (_ ("corrupt loose object '%s'" ), oid_to_hex (oid ));
1447- else if (stream -> avail_in )
1442+ FREE_AND_NULL (buf );
1443+ } else if (stream -> avail_in ) {
14481444 error (_ ("garbage at end of loose object '%s'" ),
14491445 oid_to_hex (oid ));
1450- free (buf );
1451- return NULL ;
1446+ FREE_AND_NULL (buf );
1447+ }
1448+
1449+ return buf ;
14521450}
14531451
14541452/*
@@ -1580,6 +1578,8 @@ static int loose_object_info(struct repository *r,
15801578
15811579 if (!oi -> contentp )
15821580 break ;
1581+ if (hdrbuf .len )
1582+ BUG ("unpacking content with unknown types not yet supported" );
15831583 * oi -> contentp = unpack_loose_rest (& stream , hdr , * oi -> sizep , oid );
15841584 if (* oi -> contentp )
15851585 goto cleanup ;
@@ -1600,8 +1600,8 @@ static int loose_object_info(struct repository *r,
16001600 die (_ ("loose object %s (stored in %s) is corrupt" ),
16011601 oid_to_hex (oid ), path );
16021602
1603- git_inflate_end (& stream );
16041603cleanup :
1604+ git_inflate_end (& stream );
16051605 munmap (map , mapsize );
16061606 if (oi -> sizep == & size_scratch )
16071607 oi -> sizep = NULL ;
@@ -3080,7 +3080,6 @@ static int check_stream_oid(git_zstream *stream,
30803080 git_hash_update (& c , buf , stream -> next_out - buf );
30813081 total_read += stream -> next_out - buf ;
30823082 }
3083- git_inflate_end (stream );
30843083
30853084 if (status != Z_STREAM_END ) {
30863085 error (_ ("corrupt loose object '%s'" ), oid_to_hex (expected_oid ));
@@ -3127,35 +3126,34 @@ int read_loose_object(const char *path,
31273126 if (unpack_loose_header (& stream , map , mapsize , hdr , sizeof (hdr ),
31283127 NULL ) != ULHR_OK ) {
31293128 error (_ ("unable to unpack header of %s" ), path );
3130- git_inflate_end (& stream );
3131- goto out ;
3129+ goto out_inflate ;
31323130 }
31333131
31343132 if (parse_loose_header (hdr , oi ) < 0 ) {
31353133 error (_ ("unable to parse header of %s" ), path );
3136- git_inflate_end (& stream );
3137- goto out ;
3134+ goto out_inflate ;
31383135 }
31393136
31403137 if (* oi -> typep == OBJ_BLOB && * size > big_file_threshold ) {
31413138 if (check_stream_oid (& stream , hdr , * size , path , expected_oid ) < 0 )
3142- goto out ;
3139+ goto out_inflate ;
31433140 } else {
31443141 * contents = unpack_loose_rest (& stream , hdr , * size , expected_oid );
31453142 if (!* contents ) {
31463143 error (_ ("unable to unpack contents of %s" ), path );
3147- git_inflate_end (& stream );
3148- goto out ;
3144+ goto out_inflate ;
31493145 }
31503146 hash_object_file_literally (the_repository -> hash_algo ,
31513147 * contents , * size ,
31523148 oi -> type_name -> buf , real_oid );
31533149 if (!oideq (expected_oid , real_oid ))
3154- goto out ;
3150+ goto out_inflate ;
31553151 }
31563152
31573153 ret = 0 ; /* everything checks out */
31583154
3155+ out_inflate :
3156+ git_inflate_end (& stream );
31593157out :
31603158 if (map )
31613159 munmap (map , mapsize );
0 commit comments