From 586c620609ebb485bc123bdd1da0a890cf4f5490 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 19 Feb 2015 17:54:13 -0500 Subject: [PATCH 1/3] Also treat file reading as an error if `ftell` reports invalid length. `ftell` is documented to return -1L if the function failed to get the current position indicator in the stream. The possibility is easily set aside just by checking if the function returned a negative length. --- file_ops.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/file_ops.c b/file_ops.c index e477f3d044..55d2a18cdc 100644 --- a/file_ops.c +++ b/file_ops.c @@ -110,6 +110,8 @@ static bool read_generic_file(const char *path, void **buf, ssize_t *len) fseek(file, 0, SEEK_END); _len = ftell(file); + if (_len < 0) + goto error; rewind(file); From a8fea21f119b4b466fe3471ad60d7df6510fd145 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 19 Feb 2015 17:57:49 -0500 Subject: [PATCH 2/3] additional error tracking on reading file streams if `fseek` fails --- file_ops.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/file_ops.c b/file_ops.c index 55d2a18cdc..fb874e748c 100644 --- a/file_ops.c +++ b/file_ops.c @@ -107,13 +107,15 @@ static bool read_generic_file(const char *path, void **buf, ssize_t *len) if (!file) goto error; - fseek(file, 0, SEEK_END); + if (fseek(file, 0, SEEK_END) != 0) + goto error; _len = ftell(file); if (_len < 0) goto error; - rewind(file); + if (fseek(file, 0, SEEK_SET) != 0) + goto error; rom_buf = malloc(_len + 1); From 24cbff147be8de6e6b445155385d9268d419be15 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 19 Feb 2015 18:03:57 -0500 Subject: [PATCH 3/3] additional error tracking after file reads, when closing the stream --- file_ops.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/file_ops.c b/file_ops.c index fb874e748c..2b811ccf69 100644 --- a/file_ops.c +++ b/file_ops.c @@ -133,7 +133,9 @@ static bool read_generic_file(const char *path, void **buf, ssize_t *len) /* Allow for easy reading of strings to be safe. * Will only work with sane character formatting (Unix). */ ((char*)rom_buf)[_len] = '\0'; - fclose(file); + + if (fclose(file) != 0) + RARCH_WARN("Failed to close file stream.\n"); if (len) *len = ret;