Skip to content

Commit aee7878

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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb3dc10 commit aee7878

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
@@ -271,9 +271,8 @@ struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
271271
}
272272
graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
273273
close(fd);
274-
prepare_repo_settings(r);
275-
ret = parse_commit_graph(&r->settings, graph_map, graph_size);
276274

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

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

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

391390
data = (const unsigned char *)graph_map;
@@ -405,22 +404,22 @@ struct commit_graph *parse_commit_graph(struct repo_settings *s,
405404
}
406405

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

414413
graph = alloc_commit_graph();
415414

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

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

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

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

481482
oidread(&graph->oid, graph->data + graph->data_len - graph->hash_algo->rawsz,
482-
the_repository->hash_algo);
483+
r->hash_algo);
483484

484485
free_chunkfile(cf);
485486
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)