From a6c5e310227abce8955fa1fcbf33291d33c1d2d6 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Fri, 22 Mar 2013 00:00:00 -0500 Subject: [PATCH] top: tweak 'Inspect' code for isolated edge situations If the 'Inspect' feature was used to view a file which contained binary (unprintable) data, and when the last line for such a file was purely unprintable, then that line length would be overstated by the terminal width. This was also the occasion where valgrind might object over potential reference to some unitialized value(s). It was a harmless situation and somewhat rare to begin with. Anyway this commit will eliminate the potential. Additionally, supporting readfile logic was simplified and the 'status line' bytes read is now more accurate. Signed-off-by: Jim Warner --- top/top.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/top/top.c b/top/top.c index 07a8f290..9e88ea50 100644 --- a/top/top.c +++ b/top/top.c @@ -1223,15 +1223,13 @@ static int readfile (FILE *fp, char **baddr, size_t *bsize, size_t *bread) { *bsize = READMINSZ; *baddr = alloc_c(READMINSZ); if (fp) { - while (0 < (num = fread(chunk, 1, sizeof(chunk) -1, fp))) { - if (feof(fp) && chunk[num -1]) chunk[num++] = '\0'; + while (0 < (num = fread(chunk, 1, sizeof(chunk), fp))) { *baddr = alloc_r(*baddr, num + *bsize); memcpy(*baddr + *bread, chunk, num); *bread += num; *bsize += num; }; - // adjust for the null terminator, which was counted above - if (*bread) --(*bread); + *(*baddr + *bread) = '\0'; return ferror(fp); } return ENOENT; @@ -2888,6 +2886,7 @@ static inline void insp_make_row (int col, int row) { } else { mkFND // a big show (he,he) ofs = 0; } + if (col + fr >= INSP_RLEN(row)) break; } capNO; putp(Cap_clr_eol);