From: Jim Warner Date: Fri, 22 Mar 2013 05:00:00 +0000 (-0500) Subject: top: tweak 'Inspect' code for isolated edge situations X-Git-Tag: v3.3.7~10 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a6c5e310227abce8955fa1fcbf33291d33c1d2d6;p=procps-ng 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 --- 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);