gout

A static git page generator
git clone https://git.bracken.jp/gout.git
Log | Files | Refs | README | LICENSE

commit 4bc1c3617eaf670fc84d41e1e14c2edc2cf43d3c
parent da343290aeb35f9e9800f67b311402b7d3487279
Author: Chris Bracken <chris@bracken.jp>
Date:   Wed, 20 May 2026 22:20:11 +0900

writers: default NULL paths to empty string

Calling strcmp() on a NULL string is undefined behaviour.

Diffstat:
Msrc/writer/gemini/commit.c | 19++++++++++---------
Msrc/writer/gopher/commit.c | 20+++++++++++---------
Msrc/writer/html/commit.c | 22+++++++++++-----------
3 files changed, 32 insertions(+), 29 deletions(-)

diff --git a/src/writer/gemini/commit.c b/src/writer/gemini/commit.c @@ -159,14 +159,14 @@ static void gemini_commit_write_diffstat_row(GeminiCommit* commit, fprintf(out, " %c ", delta->status); char filename[PATH_MAX]; - const char* old_file_path = delta->old_file_path; - const char* new_file_path = delta->new_file_path; + const char* old_path = delta->old_file_path ? delta->old_file_path : ""; + const char* new_path = delta->new_file_path ? delta->new_file_path : ""; int r; - if (strcmp(old_file_path, new_file_path) == 0) { - r = snprintf(filename, sizeof(filename), "%s", old_file_path); + if (strcmp(old_path, new_path) == 0) { + r = snprintf(filename, sizeof(filename), "%s", old_path); } else { - r = snprintf(filename, sizeof(filename), "%s -> %s", old_file_path, - new_file_path); + r = snprintf(filename, sizeof(filename), "%s -> %s", old_path, + new_path); } if (r < 0 || (size_t)r >= sizeof(filename)) { errx(1, "snprintf: filename truncated or error"); @@ -205,10 +205,11 @@ static void gemini_commit_write_diff_content(GeminiCommit* commit, static void gemini_commit_write_diff_delta(GeminiCommit* commit, const GitDelta* delta) { FILE* out = commit->out; + const char* old_path = delta->old_file_path ? delta->old_file_path : ""; + const char* new_path = delta->new_file_path ? delta->new_file_path : ""; fprintf(out, "=> ../file/"); - print_percent_encoded(out, delta->new_file_path); - fprintf(out, ".gmi diff --git a/%s b/%s\n", delta->old_file_path, - delta->new_file_path); + print_percent_encoded(out, new_path); + fprintf(out, ".gmi diff --git a/%s b/%s\n", old_path, new_path); if (delta->is_binary) { fprintf(out, "Binary files differ.\n\n"); diff --git a/src/writer/gopher/commit.c b/src/writer/gopher/commit.c @@ -160,14 +160,14 @@ static void gopher_commit_write_diffstat_row(GopherCommit* commit, fprintf(out, " %c ", delta->status); char filename[PATH_MAX]; - const char* old_file_path = delta->old_file_path; - const char* new_file_path = delta->new_file_path; + const char* old_path = delta->old_file_path ? delta->old_file_path : ""; + const char* new_path = delta->new_file_path ? delta->new_file_path : ""; int r; - if (strcmp(old_file_path, new_file_path) == 0) { - r = snprintf(filename, sizeof(filename), "%s", old_file_path); + if (strcmp(old_path, new_path) == 0) { + r = snprintf(filename, sizeof(filename), "%s", old_path); } else { - r = snprintf(filename, sizeof(filename), "%s -> %s", old_file_path, - new_file_path); + r = snprintf(filename, sizeof(filename), "%s -> %s", old_path, + new_path); } if (r < 0 || (size_t)r >= sizeof(filename)) { errx(1, "snprintf: filename truncated or error"); @@ -207,12 +207,14 @@ static void gopher_commit_write_diff_content(GopherCommit* commit, static void gopher_commit_write_diff_delta(GopherCommit* commit, const GitDelta* delta) { FILE* out = commit->out; + const char* old_path = delta->old_file_path ? delta->old_file_path : ""; + const char* new_path = delta->new_file_path ? delta->new_file_path : ""; fprintf(out, "[1|diff --git a/"); - print_gopher_link(out, delta->old_file_path); + print_gopher_link(out, old_path); fprintf(out, " b/"); - print_gopher_link(out, delta->new_file_path); + print_gopher_link(out, new_path); fprintf(out, "|../file/"); - print_gopher_link(out, delta->new_file_path); + print_gopher_link(out, new_path); fprintf(out, ".gph|server|port]\n"); if (delta->is_binary) { diff --git a/src/writer/html/commit.c b/src/writer/html/commit.c @@ -179,12 +179,12 @@ static void html_commit_write_diffstat_row(HtmlCommit* commit, fprintf(out, "</td>"); fprintf(out, "<td>"); fprintf(out, "<a href=\"#h%zu\">", row); - const char* old_file_path = delta->old_file_path; - const char* new_file_path = delta->new_file_path; - print_xml_encoded(out, old_file_path); - if (strcmp(old_file_path, new_file_path) != 0) { + const char* old_path = delta->old_file_path ? delta->old_file_path : ""; + const char* new_path = delta->new_file_path ? delta->new_file_path : ""; + print_xml_encoded(out, old_path); + if (strcmp(old_path, new_path) != 0) { fprintf(out, " -&gt; "); - print_xml_encoded(out, new_file_path); + print_xml_encoded(out, new_path); } fprintf(out, "</a></td>"); @@ -223,17 +223,17 @@ static void html_commit_write_diff_content(HtmlCommit* commit, static void html_commit_write_diff_delta(HtmlCommit* commit, size_t file_num, const GitDelta* delta) { - const char* old_file_path = delta->old_file_path; - const char* new_file_path = delta->new_file_path; + const char* old_path = delta->old_file_path ? delta->old_file_path : ""; + const char* new_path = delta->new_file_path ? delta->new_file_path : ""; fprintf(commit->out, "<b>diff --git "); fprintf(commit->out, "a/<a id=\"h%zu\" href=\"../file/", file_num); - print_percent_encoded(commit->out, old_file_path); + print_percent_encoded(commit->out, old_path); fprintf(commit->out, ".html\">"); - print_xml_encoded(commit->out, old_file_path); + print_xml_encoded(commit->out, old_path); fprintf(commit->out, "</a> b/<a href=\"../file/"); - print_percent_encoded(commit->out, new_file_path); + print_percent_encoded(commit->out, new_path); fprintf(commit->out, ".html\">"); - print_xml_encoded(commit->out, new_file_path); + print_xml_encoded(commit->out, new_path); fprintf(commit->out, "</a></b>\n"); if (delta->is_binary) {