Skip to content

Commit c882853

Browse files
peffgitster
authored andcommitted
commit-graph: close descriptors after mmap
We don't ever refer to the descriptor after mmap-ing it. And keeping it open means we can run out of descriptors in degenerate cases (e.g., thousands of split chain files). Let's close it as soon as possible. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b78a556 commit c882853

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

commit-graph.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ static uint8_t oid_version(void)
6969
static struct commit_graph *alloc_commit_graph(void)
7070
{
7171
struct commit_graph *g = xcalloc(1, sizeof(*g));
72-
g->graph_fd = -1;
7372

7473
return g;
7574
}
@@ -123,14 +122,13 @@ struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
123122
return NULL;
124123
}
125124
graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
126-
ret = parse_commit_graph(graph_map, fd, graph_size);
125+
close(fd);
126+
ret = parse_commit_graph(graph_map, graph_size);
127127

128128
if (ret)
129129
ret->odb = odb;
130-
else {
130+
else
131131
munmap(graph_map, graph_size);
132-
close(fd);
133-
}
134132

135133
return ret;
136134
}
@@ -165,8 +163,7 @@ static int verify_commit_graph_lite(struct commit_graph *g)
165163
return 0;
166164
}
167165

168-
struct commit_graph *parse_commit_graph(void *graph_map, int fd,
169-
size_t graph_size)
166+
struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size)
170167
{
171168
const unsigned char *data, *chunk_lookup;
172169
uint32_t i;
@@ -209,7 +206,6 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
209206

210207
graph->hash_len = the_hash_algo->rawsz;
211208
graph->num_chunks = *(unsigned char*)(data + 6);
212-
graph->graph_fd = fd;
213209
graph->data = graph_map;
214210
graph->data_len = graph_size;
215211

@@ -2129,10 +2125,9 @@ void free_commit_graph(struct commit_graph *g)
21292125
{
21302126
if (!g)
21312127
return;
2132-
if (g->graph_fd >= 0) {
2128+
if (g->data) {
21332129
munmap((void *)g->data, g->data_len);
21342130
g->data = NULL;
2135-
close(g->graph_fd);
21362131
}
21372132
free(g->filename);
21382133
free(g);

commit-graph.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ struct tree *get_commit_tree_in_graph(struct repository *r,
4040
const struct commit *c);
4141

4242
struct commit_graph {
43-
int graph_fd;
44-
4543
const unsigned char *data;
4644
size_t data_len;
4745

@@ -66,8 +64,7 @@ struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
6664
struct object_directory *odb);
6765
struct commit_graph *read_commit_graph_one(struct repository *r,
6866
struct object_directory *odb);
69-
struct commit_graph *parse_commit_graph(void *graph_map, int fd,
70-
size_t graph_size);
67+
struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size);
7168

7269
/*
7370
* Return 1 if and only if the repository has a commit-graph

fuzz-commit-graph.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "commit-graph.h"
22
#include "repository.h"
33

4-
struct commit_graph *parse_commit_graph(void *graph_map, int fd,
5-
size_t graph_size);
4+
struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size);
65

76
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
87

@@ -11,7 +10,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1110
struct commit_graph *g;
1211

1312
initialize_the_repository();
14-
g = parse_commit_graph((void *)data, -1, size);
13+
g = parse_commit_graph((void *)data, size);
1514
repo_clear(the_repository);
1615
free(g);
1716

0 commit comments

Comments
 (0)