From 20f99a8e961036c2ac8754f8352423a90159e0d0 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Mon, 18 Aug 2014 17:28:06 +0100 Subject: [PATCH] deal with ftell errors and short reads (coverity 1230589 1230588) --- gtk/viewdata.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/gtk/viewdata.c b/gtk/viewdata.c index fc7537d80..1b5bfb153 100644 --- a/gtk/viewdata.c +++ b/gtk/viewdata.c @@ -460,6 +460,7 @@ window_init_fname(const char *title, nserror ret; FILE *f; char *ndata; + long tell_len; size_t ndata_len; f = fopen(filename, "r"); @@ -471,17 +472,21 @@ window_init_fname(const char *title, return NSERROR_BAD_SIZE; } - ndata_len = ftell(f); + tell_len = ftell(f); + if (tell_len == -1) { + fclose(f); + return NSERROR_BAD_SIZE; + } if (fseek(f, 0, SEEK_SET) != 0) { fclose(f); return NSERROR_BAD_SIZE; } - ndata = malloc(ndata_len); - - fread(ndata, 1, ndata_len, f); + ndata = malloc(tell_len); + ndata_len = fread(ndata, 1, tell_len, f); + fclose(f); ret = window_init(title, leafname, ndata, ndata_len);