aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2021-08-05 18:30:08 -0300
committerEuAndreh <eu@euandre.org>2021-08-05 18:30:08 -0300
commit032628cbabebc7944fdf3fa32839102cad26a8d5 (patch)
tree3cc935a535ddad7a184a3f5b7055a3ff6d63f224 /src
parenttests/: Add integration tests, also test with Valgrind (diff)
downloadgistatic-032628cbabebc7944fdf3fa32839102cad26a8d5.tar.gz
gistatic-032628cbabebc7944fdf3fa32839102cad26a8d5.tar.xz
src/gistatic.c: Trim trailing newline of description files
Diffstat (limited to 'src')
-rw-r--r--src/gistatic.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/gistatic.c b/src/gistatic.c
index 217f3dd..cfd5c55 100644
--- a/src/gistatic.c
+++ b/src/gistatic.c
@@ -816,6 +816,56 @@ static void test_escape_html() {
}
#endif
+static void trim_newline(char *const s) {
+ if (s == NULL) {
+ return;
+ }
+
+ if (s[0] == '\0') {
+ return;
+ }
+
+ const size_t len = strlen(s);
+ if (s[len - 1] == '\n') {
+ s[len - 1] = '\0';
+ }
+}
+
+#ifdef TEST
+static void test_trim_newline() {
+ test_start("test_trim_newline");
+ {
+ testing("empty string");
+ char *const s = "";
+ trim_newline(s);
+ assert(strcmp(s, "") == 0);
+ test_ok();
+ }
+ {
+ testing("NULL string");
+ char *const s = NULL;
+ trim_newline(s);
+ assert(s == NULL);
+ test_ok();
+ }
+ {
+ testing("string without a newline");
+ char *const s = "a string without a newline";
+ trim_newline(s);
+ assert(strcmp(s, "a string without a newline") == 0);
+ test_ok();
+ }
+ return;
+ {
+ testing("string with a newline");
+ char *const s = "a string with an ending newline\n";
+ trim_newline(s);
+ assert(strcmp(s, "a string with an ending newline") == 0);
+ test_ok();
+ }
+}
+#endif
+
static int index_write_header(FILE *const fd, const char *const idx_title) {
const int e = fprintf(
fd,
@@ -1042,6 +1092,8 @@ static int index_write_row(FILE *const fd, char *const repopath) {
if (!fgets(description, sizeof(description), f)) {
logerrs("fgets(\"", description_path, "\")",
strerror(errno), __LINE__);
+ } else {
+ trim_newline(description);
}
if (fclose(f)) {
logerrs("fclose(\"", description_path, "\")",
@@ -1737,6 +1789,8 @@ static int repo_write(
if (!fgets(description, sizeof(description), f)) {
logerrs("fget(\"", description_path, "\")",
strerror(errno), __LINE__);
+ } else {
+ trim_newline(description);
}
if (fclose(f)) {
logerrs("fclose(\"", description_path, "\")",
@@ -1797,6 +1851,7 @@ static void unit_tests(){
test_strjoin();
test_formatted_date();
test_escape_html();
+ test_trim_newline();
test_last_commit_date();
git_libgit2_shutdown();
}