cgit

commit 6a8d6d4b5021af6c90ca0da806691987df449469

Author: Jason A. Donenfeld <Jason@zx2c4.com>

global: use proper accessors for maybe_tree

A previous commit changed ->tree to ->maybe_tree throughout, which may
have worked at the time, but wasn't safe, because maybe_tree is loaded
lazily. This manifested itself in crashes when using the "follow" log
feature. The proper fix is to use the correct contextual accessors
everytime we want access to maybe_tree. Thankfully, the commit.cocci
script takes care of creating mostly-correct patches that we could then
fix up, resulting in this commit here.

Fixes: 255b78f ("git: update to v2.18.0")
Reviewed-by: Christian Hesse <mail@eworm.de>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>

 ui-blame.c | 6 ++++--
 ui-blob.c | 17 +++++++++++------
 ui-commit.c | 2 +-
 ui-diff.c | 4 ++--
 ui-log.c | 4 ++--
 ui-plain.c | 7 ++++---
 ui-tree.c | 8 +++++---


diff --git a/ui-blame.c b/ui-blame.c
index 644c30ad289e79f98b59b49aac1aef18028e9118..f28eea0c74e82e2e67c686496db9905478ac085b 100644
--- a/ui-blame.c
+++ b/ui-blame.c
@@ -290,8 +290,10 @@ 	walk_tree_ctx.curr_rev = xstrdup(rev);
 	walk_tree_ctx.match_baselen = (path_items.match) ?
 				       basedir_len(path_items.match) : -1;
 
-	read_tree_recursive(the_repository, commit->maybe_tree, "", 0, 0,
-		&paths, walk_tree, &walk_tree_ctx);
+	read_tree_recursive(the_repository,
+			    repo_get_commit_tree(the_repository, commit),
+			    "", 0, 0,
+			    &paths, walk_tree, &walk_tree_ctx);
 	if (!walk_tree_ctx.state)
 		cgit_print_error_page(404, "Not found", "Not found");
 	else if (walk_tree_ctx.state == 2)




diff --git a/ui-blob.c b/ui-blob.c
index 30e2d4bf5f5484e1a5cd3541e8531f110a1e91f0..f76c641e355bda747dc74e9bfcaa727de284eaaf 100644
--- a/ui-blob.c
+++ b/ui-blob.c
@@ -56,8 +56,9 @@ 	if (get_oid(ref, &oid))
 		goto done;
 	if (oid_object_info(the_repository, &oid, &size) != OBJ_COMMIT)
 		goto done;
-	read_tree_recursive(the_repository, lookup_commit_reference(the_repository, &oid)->maybe_tree,
-		"", 0, 0, &paths, walk_tree, &walk_tree_ctx);
+	read_tree_recursive(the_repository,
+			    repo_get_commit_tree(the_repository, lookup_commit_reference(the_repository, &oid)),
+			    "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
 
 done:
 	free(path_items.match);
@@ -91,8 +92,10 @@ 		return -1;
 	type = oid_object_info(the_repository, &oid, &size);
 	if (type == OBJ_COMMIT) {
 		commit = lookup_commit_reference(the_repository, &oid);
-		read_tree_recursive(the_repository, commit->maybe_tree,
-			"", 0, 0, &paths, walk_tree, &walk_tree_ctx);
+		read_tree_recursive(the_repository,
+				    repo_get_commit_tree(the_repository, commit),
+				    "", 0, 0, &paths, walk_tree,
+				    &walk_tree_ctx);
 		if (!walk_tree_ctx.found_path)
 			return -1;
 		type = oid_object_info(the_repository, &oid, &size);
@@ -148,8 +151,10 @@ 	type = oid_object_info(the_repository, &oid, &size);
 
 	if ((!hex) && type == OBJ_COMMIT && path) {
 		commit = lookup_commit_reference(the_repository, &oid);
-		read_tree_recursive(the_repository, commit->maybe_tree,
-			"", 0, 0, &paths, walk_tree, &walk_tree_ctx);
+		read_tree_recursive(the_repository,
+				    repo_get_commit_tree(the_repository, commit),
+				    "", 0, 0, &paths, walk_tree,
+				    &walk_tree_ctx);
 		type = oid_object_info(the_repository, &oid, &size);
 	}
 




diff --git a/ui-commit.c b/ui-commit.c
index 9a47b54c560544cf32e0b797614e04c6606eff80..783211ff75deb49aff982e25711efc062f8885c1 100644
--- a/ui-commit.c
+++ b/ui-commit.c
@@ -78,7 +78,7 @@ 	cgit_patch_link("patch", NULL, NULL, NULL, tmp, prefix);
 	html(")</td></tr>\n");
 	html("<tr><th>tree</th><td colspan='2' class='sha1'>");
 	tmp = xstrdup(hex);
-	cgit_tree_link(oid_to_hex(&commit->maybe_tree->object.oid), NULL, NULL,
+	cgit_tree_link(oid_to_hex(get_commit_tree_oid(commit)), NULL, NULL,
 		       ctx.qry.head, tmp, NULL);
 	if (prefix) {
 		html(" /");




diff --git a/ui-diff.c b/ui-diff.c
index c60aefd1d629826e081329713324b7b35b6dff8e..329c3506ac30e2aef6b9f4cdd935e69ef0b49768 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -413,7 +413,7 @@ 		cgit_print_error_page(404, "Not found",
 			"Bad commit: %s", oid_to_hex(new_rev_oid));
 		return;
 	}
-	new_tree_oid = &commit->maybe_tree->object.oid;
+	new_tree_oid = get_commit_tree_oid(commit);
 
 	if (old_rev) {
 		if (get_oid(old_rev, old_rev_oid)) {
@@ -434,7 +434,7 @@ 			cgit_print_error_page(404, "Not found",
 				"Bad commit: %s", oid_to_hex(old_rev_oid));
 			return;
 		}
-		old_tree_oid = &commit2->maybe_tree->object.oid;
+		old_tree_oid = get_commit_tree_oid(commit2);
 	} else {
 		old_tree_oid = NULL;
 	}




diff --git a/ui-log.c b/ui-log.c
index dc5cb1eb6a0b34e7a0cc197cb397fc002248ee1e..2939c0162a394f08ff2feefaca0a3f811ad01b00 100644
--- a/ui-log.c
+++ b/ui-log.c
@@ -153,8 +153,8 @@ 	add_lines = 0;
 	rem_lines = 0;
 
 	revs->diffopt.flags.recursive = 1;
-	diff_tree_oid(&parent->maybe_tree->object.oid,
-		      &commit->maybe_tree->object.oid,
+	diff_tree_oid(get_commit_tree_oid(parent),
+		      get_commit_tree_oid(commit),
 		      "", &revs->diffopt);
 	diffcore_std(&revs->diffopt);
 




diff --git a/ui-plain.c b/ui-plain.c
index b73c1cfed15fa0350d2004bf17107814f1165867..2a7b18ccd8c55485bf23ad75b3d13f6da65b5daa 100644
--- a/ui-plain.c
+++ b/ui-plain.c
@@ -193,13 +193,14 @@ 	}
 	if (!path_items.match) {
 		path_items.match = "";
 		walk_tree_ctx.match_baselen = -1;
-		print_dir(&commit->maybe_tree->object.oid, "", 0, "");
+		print_dir(get_commit_tree_oid(commit), "", 0, "");
 		walk_tree_ctx.match = 2;
 	}
 	else
 		walk_tree_ctx.match_baselen = basedir_len(path_items.match);
-	read_tree_recursive(the_repository, commit->maybe_tree,
-		"", 0, 0, &paths, walk_tree, &walk_tree_ctx);
+	read_tree_recursive(the_repository,
+		            repo_get_commit_tree(the_repository, commit),
+		            "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
 	if (!walk_tree_ctx.match)
 		cgit_print_error_page(404, "Not found", "Not found");
 	else if (walk_tree_ctx.match == 2)




diff --git a/ui-tree.c b/ui-tree.c
index 84eb17d6470d718dd99fbe9eb5636967bbaf2226..1e4efb253d2b44ca0adf08ee354cacd3c675e079 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -370,12 +370,14 @@
 	walk_tree_ctx.curr_rev = xstrdup(rev);
 
 	if (path == NULL) {
-		ls_tree(&commit->maybe_tree->object.oid, NULL, &walk_tree_ctx);
+		ls_tree(get_commit_tree_oid(commit), NULL, &walk_tree_ctx);
 		goto cleanup;
 	}
 
-	read_tree_recursive(the_repository, commit->maybe_tree, "", 0, 0,
-		&paths, walk_tree, &walk_tree_ctx);
+	read_tree_recursive(the_repository,
+			    repo_get_commit_tree(the_repository, commit),
+			    "", 0, 0,
+			    &paths, walk_tree, &walk_tree_ctx);
 	if (walk_tree_ctx.state == 1)
 		ls_tail();
 	else if (walk_tree_ctx.state == 2)