From 42adaa11aa0a66645d4cf94488c88660cea00c41 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Mon, 21 Sep 2026 21:34:12 -1000 Subject: Harden the page renderers --- source/ui-diff.c | 88 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 26 deletions(-) (limited to 'source/ui-diff.c') diff --git a/source/ui-diff.c b/source/ui-diff.c index fb86a96..ee8dc76 100644 --- a/source/ui-diff.c +++ b/source/ui-diff.c @@ -23,10 +23,6 @@ // and a second walk renders the page instead. #define BODY_BUDGET (8 * 1024 * 1024) -// What a context of zero means once the diff runs, mirroring the fallback in -// cgit_diff_files, so the control offers the value the diff will really use. -#define DEFAULT_CONTEXT_LINES 3 - struct fileinfo { char status; struct object_id old_oid[1]; @@ -120,7 +116,7 @@ static void print_fileinfo(struct fileinfo *info) class = "stg"; break; default: - die("bug: unhandled diff status %c", info->status); + BUG("unhandled diff status %c", info->status); } html(""); @@ -131,13 +127,18 @@ static void print_fileinfo(struct fileinfo *info) cgit_print_filemode(info->new_mode); } - if (info->old_mode != info->new_mode && !is_null_oid(info->old_oid) && !is_null_oid(info->new_oid)) { + if ( + info->old_mode != info->new_mode && + !is_null_oid(info->old_oid) && + !is_null_oid(info->new_oid) + ) { html("["); cgit_print_filemode(info->old_mode); html("]"); } htmlf("", class); - cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.oid, ctx.qry.oid2, info->new_path); + cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.oid, ctx.qry.oid2, + info->new_path); if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) { htmlf(" (%s from ", info->status == DIFF_STATUS_COPIED ? "copied" : "renamed"); html_txt(info->old_path); @@ -145,10 +146,11 @@ static void print_fileinfo(struct fileinfo *info) } html(""); if (info->binary) { - htmlf("bin%lu -> %lu bytes\n", info->old_size, info->new_size); + htmlf("bin%lu -> %lu bytes\n", info->old_size, + info->new_size); return; } - htmlf("%d", info->added + info->removed); + htmlf("%u", info->added + info->removed); html(""); html(""); add_span = (int)(info->added * 1000.0 / scale + 0.5); @@ -173,7 +175,11 @@ static void count_diff_lines(char *line, int len) } if (!render_line_fn || render_suppressed) return; - if (cap_diffs && ctx.cfg.max_diff_lines > 0 && lines_added + lines_removed > ctx.cfg.max_diff_lines) { + if ( + cap_diffs && + ctx.cfg.max_diff_lines > 0 && + lines_added + lines_removed > ctx.cfg.max_diff_lines + ) { render_suppressed = 1; return; } @@ -184,7 +190,8 @@ static int show_filepair(struct diff_filepair *pair) { if (!current_prefix) return 1; - return starts_with(pair->one->path, current_prefix) || starts_with(pair->two->path, current_prefix); + return starts_with(pair->one->path, current_prefix) || + starts_with(pair->two->path, current_prefix); } /* @@ -220,8 +227,8 @@ static char *abbrev_oid(const struct object_id *oid) return xstrdup(repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV)); } -static void print_file_header(const struct object_id *old_oid, char *old_path, int old_mode, - const struct object_id *new_oid, char *new_path, int new_mode) +static void print_file_header(const struct object_id *old_oid, char *old_path, unsigned old_mode, + const struct object_id *new_oid, char *new_path, unsigned new_mode) { char *old_abbrev, *new_abbrev; int subproject; @@ -253,8 +260,9 @@ static void print_file_header(const struct object_id *old_oid, char *old_path, i if (is_null_oid(old_oid)) { old_path = "dev/null"; html("
--- /"); - } else + } else { html("
--- a/"); + } if (old_mode != 0) cgit_tree_link(old_path, NULL, NULL, ctx.qry.head, oid_to_hex(old_rev_oid), old_path); else @@ -262,8 +270,9 @@ static void print_file_header(const struct object_id *old_oid, char *old_path, i if (is_null_oid(new_oid)) { new_path = "dev/null"; html("
+++ /"); - } else + } else { html("
+++ b/"); + } if (new_mode != 0) cgit_tree_link(new_path, NULL, NULL, ctx.qry.head, oid_to_hex(new_rev_oid), new_path); else @@ -303,13 +312,15 @@ static void print_truncated(const char *path) else html("
"); html("This diff is too large to be rendered inline. "); - cgit_diff_link("View it on its own page", NULL, NULL, ctx.qry.head, ctx.qry.oid, ctx.qry.oid2, path); + cgit_diff_link("View it on its own page", NULL, NULL, ctx.qry.head, ctx.qry.oid, ctx.qry.oid2, + path); html("."); if (use_ssdiff) { html("
"); cgit_ssdiff_footer(); - } else + } else { html(""); + } } static struct fileinfo *reserve_item(void) @@ -552,7 +563,7 @@ void cgit_print_diff_ctrls(void) html("