@matheusbsbs / matheus-bagliano / commits / c8828530b7
Fork 0

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 <peff@peff.net> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 23, 2020 at 15:41 UTC c8828530b7797f5ab584c84dc2b86d3c14b39c8d
3 files changed +8 -17
commit-graph.c
+5 -10
@@ -69,7 +69,6 @@ static uint8_t oid_version(void)
69 static struct commit_graph *alloc_commit_graph(void)
70 {
71 struct commit_graph *g = xcalloc(1, sizeof(*g));
72 - g->graph_fd = -1;
72
73 return g;
74 }
@@ -123,14 +122,13 @@ struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
122 return NULL;
123 }
124 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);
127
128 if (ret)
129 ret->odb = odb;
130 - else {
130 + else
131 munmap(graph_map, graph_size);
132 - close(fd);
133 - }
132
133 return ret;
134 }
@@ -165,8 +163,7 @@ static int verify_commit_graph_lite(struct commit_graph *g)
163 return 0;
164 }
165
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)
167 {
168 const unsigned char *data, *chunk_lookup;
169 uint32_t i;
@@ -209,7 +206,6 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
206
207 graph->hash_len = the_hash_algo->rawsz;
208 graph->num_chunks = *(unsigned char*)(data + 6);
212 - graph->graph_fd = fd;
209 graph->data = graph_map;
210 graph->data_len = graph_size;
211
@@ -2129,10 +2125,9 @@ void free_commit_graph(struct commit_graph *g)
2125 {
2126 if (!g)
2127 return;
2132 - if (g->graph_fd >= 0) {
2128 + if (g->data) {
2129 munmap((void *)g->data, g->data_len);
2130 g->data = NULL;
2135 - close(g->graph_fd);
2131 }
2132 free(g->filename);
2133 free(g);
commit-graph.h
+1 -4
@@ -40,8 +40,6 @@ struct tree *get_commit_tree_in_graph(struct repository *r,
40 const struct commit *c);
41
42 struct commit_graph {
43 - int graph_fd;
44 -
43 const unsigned char *data;
44 size_t data_len;
45
@@ -66,8 +64,7 @@ struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
64 struct object_directory *odb);
65 struct commit_graph *read_commit_graph_one(struct repository *r,
66 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);
68
69 /*
70 * Return 1 if and only if the repository has a commit-graph
fuzz-commit-graph.c
+2 -3
@@ -1,8 +1,7 @@
1 #include "commit-graph.h"
2 #include "repository.h"
3
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);
5
6 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
7
@@ -11,7 +10,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
10 struct commit_graph *g;
11
12 initialize_the_repository();
14 - g = parse_commit_graph((void *)data, -1, size);
13 + g = parse_commit_graph((void *)data, size);
14 repo_clear(the_repository);
15 free(g);
16