summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Newbold <sam@rwsh.org>2019-06-10 16:29:37 +0200
committerSam Newbold <sam@rwsh.org>2019-06-10 16:36:18 +0200
commit300b31b2dfb3ee7a84c78bd2802cb5649d0dc1ba (patch)
treeb6e35d33b4aec49343a88a046268684b8d697be6
parent9b5f6ae5f460c8642286cf174751a317f155d043 (diff)
print_file_contents: allocate enough to include \0
related to #331 Previously, if max_chars was read, the null byte would be written past the end of buf.
-rw-r--r--src/print_file_contents.c2
-rw-r--r--testcases/025-file-contents/expected_output.txt2
-rw-r--r--testcases/025-file-contents/i3status.conf6
3 files changed, 8 insertions, 2 deletions
diff --git a/src/print_file_contents.c b/src/print_file_contents.c
index afbe3e3..91ad80e 100644
--- a/src/print_file_contents.c
+++ b/src/print_file_contents.c
@@ -23,7 +23,7 @@ static void *scalloc(size_t size) {
void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_bad, const int max_chars) {
const char *walk = format;
char *outwalk = buffer;
- char *buf = scalloc(max_chars * sizeof(char));
+ char *buf = scalloc(max_chars * sizeof(char) + 1);
int n = -1;
int fd = open(path, O_RDONLY);
diff --git a/testcases/025-file-contents/expected_output.txt b/testcases/025-file-contents/expected_output.txt
index 5c56020..b3fb8e5 100644
--- a/testcases/025-file-contents/expected_output.txt
+++ b/testcases/025-file-contents/expected_output.txt
@@ -1 +1 @@
-contents | NONEXISTANT - 2: No such file or directory
+contents | con | NONEXISTANT - 2: No such file or directory
diff --git a/testcases/025-file-contents/i3status.conf b/testcases/025-file-contents/i3status.conf
index 26646cd..78f9f50 100644
--- a/testcases/025-file-contents/i3status.conf
+++ b/testcases/025-file-contents/i3status.conf
@@ -3,12 +3,18 @@ general {
}
order += "read_file EXISTING"
+order += "read_file TRUNCATED"
order += "read_file NONEXISTANT"
read_file EXISTING {
path = "testcases/025-file-contents/short.txt"
}
+read_file TRUNCATED {
+ path = "testcases/025-file-contents/short.txt"
+ max_characters = 3
+}
+
read_file NONEXISTANT {
path = "testcases/025-file-contents/nonexistant"
}