]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit-graph: close descriptors after mmap
authorJeff King <peff@peff.net>
Thu, 23 Apr 2020 21:41:13 +0000 (15:41 -0600)
committerJunio C Hamano <gitster@pobox.com>
Sat, 25 Apr 2020 05:25:50 +0000 (22:25 -0700)
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>
commit-graph.c
commit-graph.h
fuzz-commit-graph.c

index 1a16d49bcbe40fdf77161bca14340e06df90ca5e..ac6ba5af4578dbd693abcb7d17b455cdc7402807 100644 (file)
@@ -69,7 +69,6 @@ static uint8_t oid_version(void)
 static struct commit_graph *alloc_commit_graph(void)
 {
        struct commit_graph *g = xcalloc(1, sizeof(*g));
-       g->graph_fd = -1;
 
        return g;
 }
@@ -123,14 +122,13 @@ struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
                return NULL;
        }
        graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
-       ret = parse_commit_graph(graph_map, fd, graph_size);
+       close(fd);
+       ret = parse_commit_graph(graph_map, graph_size);
 
        if (ret)
                ret->odb = odb;
-       else {
+       else
                munmap(graph_map, graph_size);
-               close(fd);
-       }
 
        return ret;
 }
@@ -165,8 +163,7 @@ static int verify_commit_graph_lite(struct commit_graph *g)
        return 0;
 }
 
-struct commit_graph *parse_commit_graph(void *graph_map, int fd,
-                                       size_t graph_size)
+struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size)
 {
        const unsigned char *data, *chunk_lookup;
        uint32_t i;
@@ -209,7 +206,6 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
 
        graph->hash_len = the_hash_algo->rawsz;
        graph->num_chunks = *(unsigned char*)(data + 6);
-       graph->graph_fd = fd;
        graph->data = graph_map;
        graph->data_len = graph_size;
 
@@ -2129,10 +2125,9 @@ void free_commit_graph(struct commit_graph *g)
 {
        if (!g)
                return;
-       if (g->graph_fd >= 0) {
+       if (g->data) {
                munmap((void *)g->data, g->data_len);
                g->data = NULL;
-               close(g->graph_fd);
        }
        free(g->filename);
        free(g);
index 98ef121924486dc9442a1fa5b70cea5efd623e2d..1254eae94870207f18df5289ca243d18a0fc7649 100644 (file)
@@ -40,8 +40,6 @@ struct tree *get_commit_tree_in_graph(struct repository *r,
                                      const struct commit *c);
 
 struct commit_graph {
-       int graph_fd;
-
        const unsigned char *data;
        size_t data_len;
 
@@ -66,8 +64,7 @@ struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
                                                 struct object_directory *odb);
 struct commit_graph *read_commit_graph_one(struct repository *r,
                                           struct object_directory *odb);
-struct commit_graph *parse_commit_graph(void *graph_map, int fd,
-                                       size_t graph_size);
+struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size);
 
 /*
  * Return 1 if and only if the repository has a commit-graph
index 0157acbf2e0554aaa7b3b6750a4d2bdc1ff89abd..9fd1c04edd3108917ebae6e83840e1a2383606a9 100644 (file)
@@ -1,8 +1,7 @@
 #include "commit-graph.h"
 #include "repository.h"
 
-struct commit_graph *parse_commit_graph(void *graph_map, int fd,
-                                       size_t graph_size);
+struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size);
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
 
@@ -11,7 +10,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
        struct commit_graph *g;
 
        initialize_the_repository();
-       g = parse_commit_graph((void *)data, -1, size);
+       g = parse_commit_graph((void *)data, size);
        repo_clear(the_repository);
        free(g);