git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 5/5] commit-graph.h: use odb in 'load_commit_graph_one_fd_st'

From
Taylor Blau <me@ttaylorr.com>
Date
Feb 3, 2020, 21:18 UTC
Message-ID
<aa12b7378b90abff5cb288a64fbe4a1b335cf73b.1580764494.git.me@ttaylorr.com>
In-Reply-To
<cover.1580764494.git.me@ttaylorr.com>

Apply a similar treatment as in the previous patch to pass a 'struct object_directory *' through the 'load_commit_graph_one_fd_st' initializer, too.

This prevents a potential bug where a pointer comparison is made to a NULL 'g->odb', which would cause the commit-graph machinery to think that a pair of commit-graphs belonged to different alternates when in fact they do not (i.e., in the case of no '--object-dir').

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 builtin/commit-graph.c     |  2 +-
 commit-graph.c             | 21 ++++++++++-----------
 commit-graph.h             |  3 ++-
 t/helper/test-read-graph.c |  2 +-
 4 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index b16eba2a7a..dd6ab84be8 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -95,7 +95,7 @@ static int graph_verify(int argc, const char **argv)
 	FREE_AND_NULL(graph_name);
 
 	if (open_ok)
-		graph = load_commit_graph_one_fd_st(fd, &st);
+		graph = load_commit_graph_one_fd_st(fd, &st, odb);
 	else
 		graph = read_commit_graph_one(the_repository, odb);
 
diff --git a/commit-graph.c b/commit-graph.c
index 49541760b5..656dd647d5 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -108,7 +108,8 @@ int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
 	return 1;
 }
 
-struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
+struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
+						 struct object_directory *odb)
 {
 	void *graph_map;
 	size_t graph_size;
@@ -124,7 +125,9 @@ struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
 	graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
 	ret = parse_commit_graph(graph_map, fd, graph_size);
 
-	if (!ret) {
+	if (ret)
+		ret->odb = odb;
+	else {
 		munmap(graph_map, graph_size);
 		close(fd);
 	}
@@ -299,7 +302,8 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
 	return graph;
 }
 
-static struct commit_graph *load_commit_graph_one(const char *graph_file)
+static struct commit_graph *load_commit_graph_one(const char *graph_file,
+						  struct object_directory *odb)
 {
 
 	struct stat st;
@@ -310,7 +314,7 @@ static struct commit_graph *load_commit_graph_one(const char *graph_file)
 	if (!open_ok)
 		return NULL;
 
-	g = load_commit_graph_one_fd_st(fd, &st);
+	g = load_commit_graph_one_fd_st(fd, &st, odb);
 
 	if (g)
 		g->filename = xstrdup(graph_file);
@@ -322,12 +326,9 @@ static struct commit_graph *load_commit_graph_v1(struct repository *r,
 						 struct object_directory *odb)
 {
 	char *graph_name = get_commit_graph_filename(odb);
-	struct commit_graph *g = load_commit_graph_one(graph_name);
+	struct commit_graph *g = load_commit_graph_one(graph_name, odb);
 	free(graph_name);
 
-	if (g)
-		g->odb = odb;
-
 	return g;
 }
 
@@ -406,13 +407,11 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r,
 		valid = 0;
 		for (odb = r->objects->odb; odb; odb = odb->next) {
 			char *graph_name = get_split_graph_filename(odb, line.buf);
-			struct commit_graph *g = load_commit_graph_one(graph_name);
+			struct commit_graph *g = load_commit_graph_one(graph_name, odb);
 
 			free(graph_name);
 
 			if (g) {
-				g->odb = odb;
-
 				if (add_graph_to_chain(g, graph_chain, oids, i)) {
 					graph_chain = g;
 					valid = 1;
diff --git a/commit-graph.h b/commit-graph.h
index 5a690723b0..e87a6f6360 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -61,7 +61,8 @@ struct commit_graph {
 	const unsigned char *chunk_base_graphs;
 };
 
-struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st);
+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,
diff --git a/t/helper/test-read-graph.c b/t/helper/test-read-graph.c
index 2c2f65f06c..f8a461767c 100644
--- a/t/helper/test-read-graph.c
+++ b/t/helper/test-read-graph.c
@@ -22,7 +22,7 @@ int cmd__read_graph(int argc, const char **argv)
 	if (!open_ok)
 		die_errno(_("Could not open commit-graph '%s'"), graph_name);
 
-	graph = load_commit_graph_one_fd_st(fd, &st);
+	graph = load_commit_graph_one_fd_st(fd, &st, odb);
 	if (!graph)
 		return 1;
 
-- 
2.25.0.119.gaa12b7378b
Previous: Junio C HamanoNext: Taylor Blau
Message 25 of 28 in “commit-graph: use 'struct object_directory *' everywhere”
  1. 0/6 commit-graph: use 'struct object_directory *' everywhereTaylor Blau, Jan 30, 2020
  2. 1/6 t5318: don't pass non-object directory to '--object-dir'Taylor Blau, Jan 30, 2020
  3. 5/6 commit-graph.c: remove path normalization, comparisonTaylor Blau, Jan 30, 2020
  4. 6/6 commit-graph.h: use odb in 'load_commit_graph_one_fd_st'Taylor Blau, Jan 30, 2020
  5. 2/6 commit-graph.h: store object directory in 'struct commit_graph'Taylor Blau, Jan 30, 2020
  6. Martin ÅgrenJan 31, 2020
  7. Jeff KingJan 31, 2020
  8. Martin ÅgrenJan 31, 2020
  9. Taylor BlauFeb 3, 2020
  10. Jeff KingFeb 3, 2020
  11. Junio C HamanoJan 31, 2020
  12. Taylor BlauFeb 3, 2020
  13. 4/6 commit-graph.h: store an odb in 'struct write_commit_graph_context'Taylor Blau, Jan 30, 2020
  14. 3/6 builtin/commit-graph.c: die() with unknown '--object-dir'Taylor Blau, Jan 30, 2020
  15. Jeff KingJan 31, 2020
  16. Derrick StoleeJan 31, 2020
  17. Taylor BlauFeb 3, 2020
  18. 0/5 commit-graph: use 'struct object_directory *' everywhereTaylor Blau, Feb 3, 2020
  19. 1/5 t5318: don't pass non-object directory to '--object-dir'Taylor Blau, Feb 3, 2020
  20. 2/5 commit-graph.h: store an odb in 'struct write_commit_graph_context'Taylor Blau, Feb 3, 2020
  21. 2/5 commit-graph.h: store an odb in 'struct write_commit_graph_context'Taylor Blau, Feb 4, 2020
  22. Junio C HamanoFeb 4, 2020
  23. Taylor BlauFeb 4, 2020
  24. Junio C HamanoFeb 4, 2020
  25. 5/5 commit-graph.h: use odb in 'load_commit_graph_one_fd_st'Taylor Blau, Feb 3, 2020
  26. 4/5 commit-graph.c: remove path normalization, comparisonTaylor Blau, Feb 3, 2020
  27. 3/5 commit-graph.h: store object directory in 'struct commit_graph'Taylor Blau, Feb 3, 2020
  28. Jeff KingFeb 5, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.