Skip to content

Commit fbda77c

Browse files
jonathantanmygitster
authored andcommitted
commit-graph: avoid memory leaks
A fuzzer running on the entry point provided by fuzz-commit-graph.c revealed a memory leak when parse_commit_graph() creates a struct bloom_filter_settings and then returns early due to error. Fix that error by always freeing that struct first (if it exists) before returning early due to error. While making that change, I also noticed another possible memory leak - when the BLOOMDATA chunk is provided but not BLOOMINDEXES. Also fix that error. Signed-off-by: Jonathan Tan <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent caf388c commit fbda77c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

commit-graph.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
271271
if (data + graph_size - chunk_lookup <
272272
GRAPH_CHUNKLOOKUP_WIDTH) {
273273
error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
274-
free(graph);
275-
return NULL;
274+
goto free_and_return;
276275
}
277276

278277
chunk_id = get_be32(chunk_lookup + 0);
@@ -283,8 +282,7 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
283282
if (chunk_offset > graph_size - the_hash_algo->rawsz) {
284283
error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
285284
(uint32_t)chunk_offset);
286-
free(graph);
287-
return NULL;
285+
goto free_and_return;
288286
}
289287

290288
switch (chunk_id) {
@@ -351,8 +349,7 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
351349

352350
if (chunk_repeated) {
353351
error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
354-
free(graph);
355-
return NULL;
352+
goto free_and_return;
356353
}
357354

358355
if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
@@ -371,17 +368,20 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
371368
/* We need both the bloom chunks to exist together. Else ignore the data */
372369
graph->chunk_bloom_indexes = NULL;
373370
graph->chunk_bloom_data = NULL;
374-
graph->bloom_filter_settings = NULL;
371+
FREE_AND_NULL(graph->bloom_filter_settings);
375372
}
376373

377374
hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
378375

379-
if (verify_commit_graph_lite(graph)) {
380-
free(graph);
381-
return NULL;
382-
}
376+
if (verify_commit_graph_lite(graph))
377+
goto free_and_return;
383378

384379
return graph;
380+
381+
free_and_return:
382+
free(graph->bloom_filter_settings);
383+
free(graph);
384+
return NULL;
385385
}
386386

387387
static struct commit_graph *load_commit_graph_one(const char *graph_file,

0 commit comments

Comments
 (0)