1
Author: <hesso@pool.math.tu-berlin.de>
2
Description: Prevent top from segfaulting when the display is shrinked to only
3
a few (read: 3 or less) lines.
5
===================================================================
6
--- a/top.c 2009-11-24 21:00:35.000000000 +1100
7
+++ b/top.c 2009-11-24 21:00:39.000000000 +1100
9
are exploited in a macro and represent 90% of our optimization.
10
The Stdout_buf is transparent to our code and regardless of whose
11
buffer is used, stdout is flushed at frame end or if interactive. */
12
-static char *Pseudo_scrn;
13
+static PSEUDO_SCREEN_t Pseudo_scrn;
14
static int Pseudo_row, Pseudo_cols, Pseudo_size;
16
// less than stdout's normal buffer but with luck mostly '\n' anyway
17
@@ -2431,7 +2431,10 @@
18
Pseudo_cols = Screen_cols + CLRBUFSIZ + 1;
19
if (Batch) Pseudo_size = ROWBUFSIZ + 1;
20
else Pseudo_size = Pseudo_cols * Screen_rows;
21
- Pseudo_scrn = alloc_r(Pseudo_scrn, Pseudo_size);
22
+ if( Pseudo_scrn.buf == NULL || Pseudo_size > Pseudo_scrn.mem_size ) {
23
+ Pseudo_scrn.buf = alloc_r(Pseudo_scrn.buf, Pseudo_size);
24
+ Pseudo_scrn.mem_size = Pseudo_size;
27
// force rebuild of column headers AND libproc/readproc requirements
30
// reframewins(), who also builds each window's column headers
31
if (!Frames_libflags) {
33
- memset(Pseudo_scrn, '\0', Pseudo_size);
34
+ memset(Pseudo_scrn.buf, '\0', Pseudo_size);
36
Pseudo_row = Msg_row = scrlins = 0;
39
===================================================================
40
--- a/top.h 2009-11-24 20:53:04.000000000 +1100
41
+++ b/top.h 2009-11-24 21:00:39.000000000 +1100
43
int _len = 1 + snprintf(_str, sizeof(_str), fmt, ## arg); \
44
putp ( Batch ? _str : \
46
- char *restrict const _pse = &Pseudo_scrn[Pseudo_row++ * Pseudo_cols]; \
47
+ char *restrict const _pse = &Pseudo_scrn.buf[Pseudo_row++ * Pseudo_cols]; \
48
memcmp(_pse, _str, _len) ? memcpy(_pse, _str, _len) : "\n"; \
52
int _len = 1 + snprintf(_str, sizeof(_str), fmt, ## arg); \
53
if (Batch) _ptr = _str; \
55
- _ptr = &Pseudo_scrn[Pseudo_row++ * Pseudo_cols]; \
56
+ if (Pseudo_row * Pseudo_cols + _len > Pseudo_size) { \
57
+ Pseudo_scrn.buf = realloc(Pseudo_scrn.buf, Pseudo_row * Pseudo_cols + _len); \
58
+ Pseudo_scrn.mem_size = Pseudo_size = Pseudo_row * Pseudo_cols + _len; \
60
+ _ptr = &Pseudo_scrn.buf[Pseudo_row++ * Pseudo_cols]; \
61
if (memcmp(_ptr, _str, _len)) { \
62
memcpy(_ptr, _str, _len); \
65
RCW_t win [4]; // a 'WIN_t.rc' for each of the 4 windows
68
+typedef struct PSEUDO_SCREEN_t {
73
// The scaling 'type' used with scale_num() -- this is how
74
// the passed number is interpreted should scaling be necessary