ansilove

ANSI and ASCII art to PNG converter in C
Log | Files | Refs | README | LICENSE

commit 8cd7c6f9bb5be31e1d38a30c5c5bdd182404fc58
parent e8f967d3ef1ba327d0d0e909316df5e562f33bb6
Author: Frederic Cambus <fred@statdns.com>
Date:   Mon, 19 Oct 2020 11:49:18 +0200

Check fread() return values in readComments() and add error handling.

Diffstat:
Msrc/sauce.c | 11+++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/sauce.c b/src/sauce.c @@ -100,10 +100,14 @@ int readComments(FILE *file, char **comment_lines, int32_t comments) { int32_t i; + size_t read_status; if (fseek(file, 0 - (RECORD_SIZE + 5 + COMMENT_SIZE *comments), SEEK_END) == 0) { char ID[6]; - fread(ID, sizeof (ID) - 1, 1, file); + read_status = fread(ID, sizeof (ID) - 1, 1, file); + if (read_status != 1) + return -1; + ID[sizeof (ID) - 1] = '\0'; if (strcmp(ID, COMMENT_ID) != 0) { @@ -113,7 +117,10 @@ readComments(FILE *file, char **comment_lines, int32_t comments) for (i = 0; i < comments; i++) { char buf[COMMENT_SIZE + 1] = ""; - fread(buf, COMMENT_SIZE, 1, file); + read_status = fread(buf, COMMENT_SIZE, 1, file); + if (read_status != 1) + return -1; + buf[COMMENT_SIZE] = '\0'; if (ferror(file) == 0) {