@@ -200,8 +200,7 @@ int git_commit_graph_file_parse(
200
200
const unsigned char * chunk_hdr ;
201
201
struct git_commit_graph_chunk * last_chunk ;
202
202
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 ;
205
204
size_t checksum_size ;
206
205
int error ;
207
206
struct git_commit_graph_chunk chunk_oid_fanout = {0 }, chunk_oid_lookup = {0 },
@@ -234,16 +233,11 @@ int git_commit_graph_file_parse(
234
233
return commit_graph_error ("wrong commit-graph size" );
235
234
memcpy (file -> checksum , (data + trailer_offset ), checksum_size );
236
235
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
-
242
236
chunk_hdr = data + sizeof (struct git_commit_graph_header );
243
237
last_chunk = NULL ;
244
238
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 ))));
247
241
if (chunk_offset < last_chunk_offset )
248
242
return commit_graph_error ("chunks are non-monotonic" );
249
243
if (chunk_offset >= trailer_offset )
@@ -331,9 +325,29 @@ int git_commit_graph_new(git_commit_graph **cgraph_out, const char *objects_dir,
331
325
return error ;
332
326
}
333
327
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
+
334
344
int git_commit_graph_open (git_commit_graph * * cgraph_out , const char * objects_dir )
335
345
{
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 ;
337
351
}
338
352
339
353
int git_commit_graph_file_open (git_commit_graph_file * * file_out , const char * path )
0 commit comments