Skip to content

Commit f1141b4

Browse files
pks-tgitster
authored andcommitted
commit-graph: refactor parse_commit_graph() to take a repository
Refactor `parse_commit_graph()` so that it takes a repository instead of taking repository settings. On the one hand this allows us to get rid of instances where we access `the_hash_algo` by using the repository's hash algorithm instead. On the other hand it also allows us to move the call of `prepare_repo_settings()` into the function itself. Note that there's one small catch, as the commit-graph fuzzer calls this function directly without having a fully functional repository at hand. And while the fuzzer already initializes `the_repository` with relevant info, the call to `prepare_repo_settings()` would fail because we don't have a fully-initialized repository. Work around the issue by also settings `settings.initialized` to pretend that we've already read the settings. While at it, remove the redundant `parse_commit_graph()` declaration in the fuzzer. It was added together with aa65857 (commit-graph, fuzz: add fuzzer for commit-graph, 2019-01-15), but as we also declared the same function in "commit-graph.h" it wasn't ever needed. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e45402b commit f1141b4

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

commit-graph.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
272272
}
273273
graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
274274
close(fd);
275-
prepare_repo_settings(r);
276-
ret = parse_commit_graph(&r->settings, graph_map, graph_size);
277275

276+
ret = parse_commit_graph(r, graph_map, graph_size);
278277
if (ret)
279278
ret->odb_source = source;
280279
else
@@ -374,7 +373,7 @@ static int graph_read_bloom_data(const unsigned char *chunk_start,
374373
return 0;
375374
}
376375

377-
struct commit_graph *parse_commit_graph(struct repo_settings *s,
376+
struct commit_graph *parse_commit_graph(struct repository *r,
378377
void *graph_map, size_t graph_size)
379378
{
380379
const unsigned char *data;
@@ -386,7 +385,7 @@ struct commit_graph *parse_commit_graph(struct repo_settings *s,
386385
if (!graph_map)
387386
return NULL;
388387

389-
if (graph_size < graph_min_size(the_hash_algo))
388+
if (graph_size < graph_min_size(r->hash_algo))
390389
return NULL;
391390

392391
data = (const unsigned char *)graph_map;
@@ -406,22 +405,22 @@ struct commit_graph *parse_commit_graph(struct repo_settings *s,
406405
}
407406

408407
hash_version = *(unsigned char*)(data + 5);
409-
if (hash_version != oid_version(the_hash_algo)) {
408+
if (hash_version != oid_version(r->hash_algo)) {
410409
error(_("commit-graph hash version %X does not match version %X"),
411-
hash_version, oid_version(the_hash_algo));
410+
hash_version, oid_version(r->hash_algo));
412411
return NULL;
413412
}
414413

415414
graph = alloc_commit_graph();
416415

417-
graph->hash_algo = the_hash_algo;
416+
graph->hash_algo = r->hash_algo;
418417
graph->num_chunks = *(unsigned char*)(data + 6);
419418
graph->data = graph_map;
420419
graph->data_len = graph_size;
421420

422421
if (graph_size < GRAPH_HEADER_SIZE +
423422
(graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
424-
GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
423+
GRAPH_FANOUT_SIZE + r->hash_algo->rawsz) {
425424
error(_("commit-graph file is too small to hold %u chunks"),
426425
graph->num_chunks);
427426
free(graph);
@@ -452,7 +451,9 @@ struct commit_graph *parse_commit_graph(struct repo_settings *s,
452451
pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
453452
&graph->chunk_base_graphs_size);
454453

455-
if (s->commit_graph_generation_version >= 2) {
454+
prepare_repo_settings(r);
455+
456+
if (r->settings.commit_graph_generation_version >= 2) {
456457
read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
457458
graph_read_generation_data, graph);
458459
pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
@@ -463,7 +464,7 @@ struct commit_graph *parse_commit_graph(struct repo_settings *s,
463464
graph->read_generation_data = 1;
464465
}
465466

466-
if (s->commit_graph_changed_paths_version) {
467+
if (r->settings.commit_graph_changed_paths_version) {
467468
read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
468469
graph_read_bloom_index, graph);
469470
read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
@@ -480,7 +481,7 @@ struct commit_graph *parse_commit_graph(struct repo_settings *s,
480481
}
481482

482483
oidread(&graph->oid, graph->data + graph->data_len - graph->hash_algo->rawsz,
483-
the_repository->hash_algo);
484+
r->hash_algo);
484485

485486
free_chunkfile(cf);
486487
return graph;

commit-graph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ struct repo_settings;
128128
* Callers should initialize the repo_settings with prepare_repo_settings()
129129
* prior to calling parse_commit_graph().
130130
*/
131-
struct commit_graph *parse_commit_graph(struct repo_settings *s,
131+
struct commit_graph *parse_commit_graph(struct repository *r,
132132
void *graph_map, size_t graph_size);
133133

134134
/*

oss-fuzz/fuzz-commit-graph.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include "commit-graph.h"
55
#include "repository.h"
66

7-
struct commit_graph *parse_commit_graph(struct repo_settings *s,
8-
void *graph_map, size_t graph_size);
9-
107
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
118

129
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
@@ -22,9 +19,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
2219
* possible.
2320
*/
2421
repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
22+
the_repository->settings.initialized = 1;
2523
the_repository->settings.commit_graph_generation_version = 2;
2624
the_repository->settings.commit_graph_changed_paths_version = 1;
27-
g = parse_commit_graph(&the_repository->settings, (void *)data, size);
25+
g = parse_commit_graph(the_repository, (void *)data, size);
2826
repo_clear(the_repository);
2927
free_commit_graph(g);
3028

0 commit comments

Comments
 (0)