summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Stoeckmann <tobias@stoeckmann.org>2017-04-02 13:25:14 +0200
committerTobias Stoeckmann <tobias@stoeckmann.org>2017-04-02 13:25:14 +0200
commitbdee6af09f84c224f8ba60006d3b4b977b4882e8 (patch)
tree4686b3383e1397a583786c3d553089ad81924965
parenta5e60401f9dce39f7f1bcf53ede508c63f5d2ad3 (diff)
Avoid out of boundary read on empty/broken file.
If ereadfile encounters an empty file or the file could not be read, an out ouf boundary read (and possible write) occurs. Always check the return value of fread to be > 0 before processing the result buffer. Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
-rw-r--r--src/utils.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/utils.c b/src/utils.c
index bd189d3..2c0809c 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -183,14 +183,14 @@ char *ereadfile(char *path)
{
char buffer[4096];
FILE *fp;
- int count;
+ size_t count;
fp = fopen(path, "r");
if (!fp)
return NULL;
count = fread(buffer, sizeof(char), sizeof(buffer) - 1, fp);
- if (buffer[count - 1] == '\n')
+ if (count > 0 && buffer[count - 1] == '\n')
buffer[count - 1] = '\0';
else
buffer[count] = '\0';