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

[PATCH 2/3] graph: replace assert() with graph_assert() macro

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 7, 2020, 14:55 UTC
Message-ID
<5dd305d2f0de43a70b46336c8f1a62437e0511e1.1578408947.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.517.git.1578408947.gitgitgadget@gmail.com>
From: Derrick Stolee <dstolee@microsoft.com>

The assert() macro is sometimes compiled out. Instead, switch these into BUG() statements using our own custom macro.

Reported-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 graph.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
diff --git a/graph.c b/graph.c
index aaf97069bd..ac78bdf96c 100644
--- a/graph.c
+++ b/graph.c
@@ -6,6 +6,8 @@
 #include "revision.h"
 #include "argv-array.h"
 
+#define graph_assert(exp) if (!(exp)) { BUG("assert failed: "#exp""); }
+
 /* Internal API */
 
 /*
@@ -316,7 +318,7 @@ static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void
 	struct git_graph *graph = data;
 	static struct strbuf msgbuf = STRBUF_INIT;
 
-	assert(opt);
+	graph_assert(opt);
 
 	strbuf_reset(&msgbuf);
 	if (opt->line_prefix)
@@ -865,13 +867,13 @@ static void graph_output_pre_commit_line(struct git_graph *graph,
 	 *
 	 * We need 2 extra rows for every parent over 2.
 	 */
-	assert(graph->num_parents >= 3);
+	graph_assert(graph->num_parents >= 3);
 
 	/*
 	 * graph->expansion_row tracks the current expansion row we are on.
 	 * It should be in the range [0, num_expansion_rows - 1]
 	 */
-	assert(0 <= graph->expansion_row &&
+	graph_assert(0 <= graph->expansion_row &&
 	       graph->expansion_row < graph_num_expansion_rows(graph));
 
 	/*
@@ -923,13 +925,13 @@ static void graph_output_commit_char(struct git_graph *graph, struct graph_line
 	 * (We should only see boundary commits when revs->boundary is set.)
 	 */
 	if (graph->commit->object.flags & BOUNDARY) {
-		assert(graph->revs->boundary);
+		graph_assert(graph->revs->boundary);
 		graph_line_addch(line, 'o');
 		return;
 	}
 
 	/*
-	 * get_revision_mark() handles all other cases without assert()
+	 * get_revision_mark() handles all other cases without graph_assert()
 	 */
 	graph_line_addstr(line, get_revision_mark(graph->revs, graph->commit));
 }
@@ -1094,7 +1096,7 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct graph_l
 
 			for (j = 0; j < graph->num_parents; j++) {
 				par_column = graph_find_new_column_by_commit(graph, parents->item);
-				assert(par_column >= 0);
+				graph_assert(par_column >= 0);
 
 				c = merge_chars[idx];
 				graph_line_write_column(line, &graph->new_columns[par_column], c);
@@ -1172,14 +1174,14 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
 		 * the graph much more legible, since whenever branches
 		 * cross, only one is moving directions.
 		 */
-		assert(target * 2 <= i);
+		graph_assert(target * 2 <= i);
 
 		if (target * 2 == i) {
 			/*
 			 * This column is already in the
 			 * correct place
 			 */
-			assert(graph->mapping[i] == -1);
+			graph_assert(graph->mapping[i] == -1);
 			graph->mapping[i] = target;
 		} else if (graph->mapping[i - 1] < 0) {
 			/*
@@ -1225,8 +1227,8 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
 			 * The space just to the left of this
 			 * branch should always be empty.
 			 */
-			assert(graph->mapping[i - 1] > target);
-			assert(graph->mapping[i - 2] < 0);
+			graph_assert(graph->mapping[i - 1] > target);
+			graph_assert(graph->mapping[i - 2] < 0);
 			graph->mapping[i - 2] = target;
 			/*
 			 * Mark this branch as the horizontal edge to
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Jeff King
Message 2 of 24 in “Fix two bugs in graph.c”
  1. 0/3 Fix two bugs in graph.cDerrick Stolee via GitGitGadget, Jan 7, 2020
  2. 2/3 graph: replace assert() with graph_assert() macroDerrick Stolee via GitGitGadget, Jan 7, 2020
  3. Jeff KingJan 7, 2020
  4. Eric SunshineJan 7, 2020
  5. Derrick StoleeJan 7, 2020
  6. 1/3 graph: fix case that hit assert()Derrick Stolee via GitGitGadget, Jan 7, 2020
  7. Jeff KingJan 7, 2020
  8. Derrick StoleeJan 7, 2020
  9. Junio C HamanoJan 7, 2020
  10. Jeff KingJan 7, 2020
  11. Junio C HamanoJan 7, 2020
  12. 3/3 t4215: add bigger graph collapse testDerrick Stolee via GitGitGadget, Jan 7, 2020
  13. Jeff KingJan 7, 2020
  14. Junio C HamanoJan 7, 2020
  15. Jeff KingJan 7, 2020
  16. Derrick StoleeJan 7, 2020
  17. Junio C HamanoJan 7, 2020
  18. 0/2 Fix two bugs in graph.cDerrick Stolee via GitGitGadget, Jan 7, 2020
  19. 1/2 graph: fix case that hit assert()Derrick Stolee via GitGitGadget, Jan 7, 2020
  20. Junio C HamanoJan 7, 2020
  21. Junio C HamanoJan 8, 2020
  22. Derrick StoleeJan 8, 2020
  23. 2/2 graph: fix lack of color in horizontal linesDerrick Stolee via GitGitGadget, Jan 7, 2020
  24. Junio C HamanoJan 7, 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.