@@ -200,8 +200,7 @@ int git_commit_graph_file_parse(
200200 const unsigned char * chunk_hdr ;
201201 struct git_commit_graph_chunk * last_chunk ;
202202 uint32_t i ;
203- off64_t last_chunk_offset , chunk_offset , trailer_offset ;
204- unsigned char checksum [GIT_HASH_SHA1_SIZE ];
203+ uint64_t last_chunk_offset , chunk_offset , trailer_offset ;
205204 size_t checksum_size ;
206205 int error ;
207206 struct git_commit_graph_chunk chunk_oid_fanout = {0 }, chunk_oid_lookup = {0 },
@@ -234,16 +233,11 @@ int git_commit_graph_file_parse(
234233 return commit_graph_error ("wrong commit-graph size" );
235234 memcpy (file -> checksum , (data + trailer_offset ), checksum_size );
236235
237- if (git_hash_buf (checksum , data , (size_t )trailer_offset , GIT_HASH_ALGORITHM_SHA1 ) < 0 )
238- return commit_graph_error ("could not calculate signature" );
239- if (memcmp (checksum , file -> checksum , checksum_size ) != 0 )
240- return commit_graph_error ("index signature mismatch" );
241-
242236 chunk_hdr = data + sizeof (struct git_commit_graph_header );
243237 last_chunk = NULL ;
244238 for (i = 0 ; i < hdr -> chunks ; ++ i , chunk_hdr += 12 ) {
245- chunk_offset = ((off64_t )ntohl (* ((uint32_t * )(chunk_hdr + 4 )))) << 32
246- | ((off64_t )ntohl (* ((uint32_t * )(chunk_hdr + 8 ))));
239+ chunk_offset = ((uint64_t )ntohl (* ((uint32_t * )(chunk_hdr + 4 )))) << 32
240+ | ((uint64_t )ntohl (* ((uint32_t * )(chunk_hdr + 8 ))));
247241 if (chunk_offset < last_chunk_offset )
248242 return commit_graph_error ("chunks are non-monotonic" );
249243 if (chunk_offset >= trailer_offset )
@@ -331,9 +325,29 @@ int git_commit_graph_new(git_commit_graph **cgraph_out, const char *objects_dir,
331325 return error ;
332326}
333327
328+ int git_commit_graph_validate (git_commit_graph * cgraph ) {
329+ unsigned char checksum [GIT_HASH_SHA1_SIZE ];
330+ size_t checksum_size = GIT_HASH_SHA1_SIZE ;
331+ size_t trailer_offset = cgraph -> file -> graph_map .len - checksum_size ;
332+
333+ if (cgraph -> file -> graph_map .len < checksum_size )
334+ return commit_graph_error ("map length too small" );
335+
336+ if (git_hash_buf (checksum , cgraph -> file -> graph_map .data , trailer_offset , GIT_HASH_ALGORITHM_SHA1 ) < 0 )
337+ return commit_graph_error ("could not calculate signature" );
338+ if (memcmp (checksum , cgraph -> file -> checksum , checksum_size ) != 0 )
339+ return commit_graph_error ("index signature mismatch" );
340+
341+ return 0 ;
342+ }
343+
334344int git_commit_graph_open (git_commit_graph * * cgraph_out , const char * objects_dir )
335345{
336- return git_commit_graph_new (cgraph_out , objects_dir , true);
346+ int error = git_commit_graph_new (cgraph_out , objects_dir , true);
347+ if (!error ) {
348+ return git_commit_graph_validate (* cgraph_out );
349+ }
350+ return error ;
337351}
338352
339353int git_commit_graph_file_open (git_commit_graph_file * * file_out , const char * path )
0 commit comments