diff --git a/lib/read-file.c b/lib/read-file.c index 0a15c5a..4ea38f7 100644 --- a/lib/read-file.c +++ b/lib/read-file.c @@ -65,7 +65,8 @@ fread_file (FILE * stream, size_t * length) return NULL; } - alloc = alloc_off + 1; + /* 1 > file size so eof immediately detected. */ + alloc = alloc_off + 2; buf = malloc (alloc); if (!buf) @@ -120,8 +121,10 @@ fread_file (FILE * stream, size_t * length) if (ferror (stream)) break; - /* Shrink the allocated memory if possible. */ - if (size + 1 < alloc) + /* Shrink the allocated memory if possible, + but don't bother about the extra byte + allocated to detect the eof. */ + if (size < alloc - 2) { char *smaller_buf = realloc (buf, size + 1); if (smaller_buf != NULL)