commit 5bc71cffc1e2f51a325ee31f2110adbdbc8b00e6
parent 5f9b6e2a3ceb5a98f000fd323d2470a4c8d0c859
Author: Chris Bracken <chris@bracken.jp>
Date: Wed, 23 Jul 2025 15:43:17 -0700
Use gitfile_size_bytes in place of strlen
Use gitfile_size_bytes for the maximum length of the fileblob content
rather than strlen, since it's already stored on GitFile. This is
slightly safer but also correctly handles any text file with an embedded
null character (e.g. UTF-16 encoded text).
Diffstat:
2 files changed, 29 insertions(+), 21 deletions(-)
diff --git a/writer/gopher/fileblob.c b/writer/gopher/fileblob.c
@@ -90,18 +90,22 @@ void gopher_fileblob_add_file(GopherFileBlob* blob, const GitFile* file) {
size_t i = 0;
const char* content = gitfile_content(file);
- const char* end = content + strlen(content);
+ const char* end = content + gitfile_size_bytes(file);
const char* cur_line = content;
- while (cur_line) {
- const char* next_line = strchr(cur_line, '\n');
- if (next_line || cur_line < end) {
- size_t len = (next_line ? next_line : end) - cur_line;
- i++;
- fprintf(out, "%6zu ", i);
- print_gopher_text_len(out, cur_line, len, false);
- fprintf(out, "\n");
+ while (cur_line < end) {
+ const char* next_line = memchr(cur_line, '\n', end - cur_line);
+ size_t len = (next_line ? next_line : end) - cur_line;
+
+ i++;
+ fprintf(out, "%6zu ", i);
+ print_gopher_text_len(out, cur_line, len, false);
+ fprintf(out, "\n");
+
+ if (next_line) {
+ cur_line = next_line + 1;
+ } else {
+ break;
}
- cur_line = next_line ? next_line + 1 : NULL;
}
}
diff --git a/writer/html/fileblob.c b/writer/html/fileblob.c
@@ -93,19 +93,23 @@ void html_fileblob_add_file(HtmlFileBlob* blob, const GitFile* file) {
size_t i = 0;
const char* content = gitfile_content(file);
- const char* end = content + strlen(content);
+ const char* end = content + gitfile_size_bytes(file);
const char* cur_line = content;
- while (cur_line) {
- const char* next_line = strchr(cur_line, '\n');
- if (next_line || cur_line < end) {
- size_t len = (next_line ? next_line : end) - cur_line;
- i++;
- fprintf(out, "<a href=\"#l%zu\" class=\"line\" id=\"l%zu\">", i, i);
- fprintf(out, "%7zu</a> ", i);
- print_xml_encoded_len(out, cur_line, len, false);
- fprintf(out, "\n");
+ while (cur_line < end) {
+ const char* next_line = memchr(cur_line, '\n', end - cur_line);
+ size_t len = (next_line ? next_line : end) - cur_line;
+
+ i++;
+ fprintf(out, "<a href=\"#l%zu\" class=\"line\" id=\"l%zu\">", i, i);
+ fprintf(out, "%7zu</a> ", i);
+ print_xml_encoded_len(out, cur_line, len, false);
+ fprintf(out, "\n");
+
+ if (next_line) {
+ cur_line = next_line + 1;
+ } else {
+ break;
}
- cur_line = next_line ? next_line + 1 : NULL;
}
fprintf(out, "</pre>\n");
}