commit 0a2e491aee5dffc32b4f06329a1e8e05c3fcfaf3
parent 626ff28b68ef2c1886d75aa577eb3adc0a1195f3
Author: Chris Bracken <chris@bracken.jp>
Date: Tue, 6 Jan 2026 16:29:20 +0900
Correct getline handling
Assigns the return value of getline to the correct type (ssize_t) and
checks that against -1 rather than relying on checking buf for null.
According to POSIX, there is no guarantee that getline doesn't allocate
a buffer on failure, so we need to clean up.
Diffstat:
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/src/git/repo.c b/src/git/repo.c
@@ -101,13 +101,14 @@ char* first_line_of_file(const char* path) {
}
char* buf = NULL;
size_t buf_size = 0;
- size_t len = getline(&buf, &buf_size, f);
+ ssize_t len = getline(&buf, &buf_size, f);
fclose(f);
- if (!buf) {
+ if (len == -1) {
+ free(buf);
return estrdup("");
}
// Remove trailing newline.
- if (len > 0) {
+ if (len > 0 && buf[len - 1] == '\n') {
buf[len - 1] = '\0';
}
return buf;