From 487337705a411ed5ce6c6b3064b31b7dff55cb91 Mon Sep 17 00:00:00 2001 From: Iksas Date: Fri, 30 Aug 2024 20:20:13 +0200 Subject: [PATCH 1/9] fix out-of-bounds accesses of _x_speedarray --- slurm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slurm.c b/slurm.c index f6cdf54..10fe994 100644 --- a/slurm.c +++ b/slurm.c @@ -410,7 +410,7 @@ int update_stat_large(void) */ /* move */ - for (x = GRAPH_WIDTH - 1; x >= 0; x--) { + for (x = GRAPH_WIDTH - 1; x >= 1; x--) { rx_speedarray[x] = rx_speedarray[x - 1]; tx_speedarray[x] = tx_speedarray[x - 1]; for (y = 0; y < GRAPHLARGE_HEIGHT; y++) { @@ -696,7 +696,7 @@ int update_stat_split(void) */ /* move */ - for (x = GRAPHSPLIT_WIDTH - 1; x >= 0; x--) { + for (x = GRAPHSPLIT_WIDTH - 1; x >= 1; x--) { rx_speedarray[x] = rx_speedarray[x - 1]; tx_speedarray[x] = tx_speedarray[x - 1]; for (y = 0; y < GRAPHSPLIT_HEIGHT; y++) { @@ -942,7 +942,7 @@ int update_stat_combined(void) */ /* move */ - for (x = GRAPHCOMBINED_WIDTH - 1; x >= 0; x--) { + for (x = GRAPHCOMBINED_WIDTH - 1; x >= 1; x--) { rx_speedarray[x] = rx_speedarray[x - 1]; tx_speedarray[x] = tx_speedarray[x - 1]; for (y = 0; y < GRAPHCOMBINED_HEIGHT; y++) { From 7a47a6374a5e55e501ab5a5cc5467405c606c414 Mon Sep 17 00:00:00 2001 From: Iksas Date: Fri, 30 Aug 2024 20:29:27 +0200 Subject: [PATCH 2/9] remove unused constants --- slurm.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/slurm.h b/slurm.h index e6e36ee..60a4a91 100644 --- a/slurm.h +++ b/slurm.h @@ -158,10 +158,6 @@ static FILE *proc_net_dev; #endif /* End of Variables Declarations */ -/* max speed in graph */ -#define GRAPHSINGLE_WIDTH 77 -#define GRAPHSINGLE_HEIGHT 10 - /* define graph height for split screen graphs */ #define GRAPHSPLIT_HEIGHT 6 #define GRAPHSPLIT_WIDTH 77 From cc6d46d0110941a1bf06899a657781745510369b Mon Sep 17 00:00:00 2001 From: Iksas Date: Fri, 30 Aug 2024 21:14:18 +0200 Subject: [PATCH 3/9] tidy up height constants --- slurm.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/slurm.h b/slurm.h index 60a4a91..c58e4c7 100644 --- a/slurm.h +++ b/slurm.h @@ -158,16 +158,18 @@ static FILE *proc_net_dev; #endif /* End of Variables Declarations */ +/* new max width and height */ +/* Note that these first two constants are used to define array sizes, + * so they must be larger/equal to all other heights/widths. */ +#define GRAPH_HEIGHT 12 +#define GRAPH_WIDTH 77 + /* define graph height for split screen graphs */ #define GRAPHSPLIT_HEIGHT 6 -#define GRAPHSPLIT_WIDTH 77 +#define GRAPHSPLIT_WIDTH GRAPH_WIDTH -#define GRAPHCOMBINED_WIDTH 77 -#define GRAPHCOMBINED_HEIGHT 12 - -/* new max height */ -#define GRAPH_HEIGHT 12 -#define GRAPH_WIDTH 77 +#define GRAPHCOMBINED_HEIGHT GRAPH_HEIGHT +#define GRAPHCOMBINED_WIDTH GRAPH_WIDTH /* large split mode graph height */ #define GRAPHLARGE_HEIGHT 11 From 01117510eaf653edee0f712e7531acff6d6529e5 Mon Sep 17 00:00:00 2001 From: Iksas Date: Fri, 30 Aug 2024 21:15:24 +0200 Subject: [PATCH 4/9] fix an error where ERR_IFACE_DOWN would never be set --- src/if_media.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/if_media.c b/src/if_media.c index 1087391..01ad732 100644 --- a/src/if_media.c +++ b/src/if_media.c @@ -270,7 +270,7 @@ int get_if_speed(char *ifstring) if (if_buf->ifOper == 1) speed = if_buf->ifSpeed / 1000; else - speed ERR_IFACE_DOWN; + speed = ERR_IFACE_DOWN; } } } From 8b673e9e4910f94dce1433f9ff1a0f7a3a72fc72 Mon Sep 17 00:00:00 2001 From: Iksas Date: Fri, 30 Aug 2024 21:21:13 +0200 Subject: [PATCH 5/9] wrap numerical constants in parantheses to prevent silent errors in the future --- error.h | 10 ++++----- os.h | 6 +++--- slurm.h | 44 +++++++++++++++++++------------------- src/hpux.c | 2 +- src/if_media.c | 8 +++---- src/if_media.h | 4 ++-- theme.h | 58 +++++++++++++++++++++++++------------------------- 7 files changed, 66 insertions(+), 66 deletions(-) diff --git a/error.h b/error.h index 9379e4c..eeaebe5 100644 --- a/error.h +++ b/error.h @@ -9,11 +9,11 @@ * (i.e. & 0x01ff to enable syslog logging) */ -#define ERR_DEBUG 0x0001 -#define ERR_NOTICE 0x0003 -#define ERR_WARNING 0x0004 -#define ERR_ERROR 0x0005 -#define ERR_FATAL 0x0006 +#define ERR_DEBUG (0x0001) +#define ERR_NOTICE (0x0003) +#define ERR_WARNING (0x0004) +#define ERR_ERROR (0x0005) +#define ERR_FATAL (0x0006) #define ERRBUF BUFSIZ #endif diff --git a/os.h b/os.h index 965ddc7..b1bbc27 100644 --- a/os.h +++ b/os.h @@ -29,7 +29,7 @@ /* set variables for extra features */ #if defined (__sun__) || defined (__sun) # ifndef __Solaris__ -# define __Solaris__ 1 +# define __Solaris__ (1) # endif #endif @@ -47,7 +47,7 @@ #ifdef __HPUX__ /* H P U X */ #define _XOPEN_SOURCE_EXTENDED -#define NO_CURSES_E 1 +#define NO_CURSES_E (1) #include #include #include @@ -200,7 +200,7 @@ #include #elif defined (__Solaris__) /* S O L A R I S */ #include -#define NO_CURSES_E 1 +#define NO_CURSES_E (1) #define _WIDEC_H #include #include diff --git a/slurm.h b/slurm.h index c58e4c7..9ad2ecd 100644 --- a/slurm.h +++ b/slurm.h @@ -22,14 +22,14 @@ * ***************************************************************************/ -#define LED_RX 1 -#define LED_TX 2 +#define LED_RX (1) +#define LED_TX (2) #ifndef TRUE -#define TRUE 1 +#define TRUE (1) #endif #ifndef FALSE -#define FALSE 0 +#define FALSE (0) #endif #define SYMBOL_TRAFFIC "x" @@ -39,9 +39,9 @@ #define PATH_NET_DEV "/proc/net/dev" #endif -#define MODE_COMBINED 1 -#define MODE_SPLIT 2 -#define MODE_LARGE 3 +#define MODE_COMBINED (1) +#define MODE_SPLIT (2) +#define MODE_LARGE (3) /* slap HPUX with a large trout * HPUX 10.xx cannot handle 2^32 int constants, so we have to "tweak" it @@ -54,17 +54,17 @@ #endif #undef MAXHOSTNAMELEN -#define MAXHOSTNAMELEN 64 +#define MAXHOSTNAMELEN (64) -#define DRAWLEN 16 +#define DRAWLEN (16) /* 10th of a second */ -#define REFRESH_DEFAULT 1 -#define REFRESH_MIN 1 -#define REFRESH_MAX 300 +#define REFRESH_DEFAULT (1) +#define REFRESH_MIN (1) +#define REFRESH_MAX (300) #ifndef BUFSIZ -#define BUFSIZ 1024 +#define BUFSIZ (1024) #warning "setting buf size to 1024" #endif @@ -141,16 +141,16 @@ void zero_stats (void); void slurm_shutdown (int); /* Variables Declarations */ -#define TYPE_MEGA 0 -#define TYPE_GIGA 1 +#define TYPE_MEGA (0) +#define TYPE_GIGA (1) int data_type = TYPE_MEGA; long refreshdelay = REFRESH_DEFAULT; /* internal database status */ /* possible modes are: */ -#define DB_STATUS_STARTUP 1 -#define DB_STATUS_RUNNING 2 -#define DB_STATUS_REINIT 3 +#define DB_STATUS_STARTUP (1) +#define DB_STATUS_RUNNING (2) +#define DB_STATUS_REINIT (3) int db_status = DB_STATUS_STARTUP; #ifdef __linux @@ -161,18 +161,18 @@ static FILE *proc_net_dev; /* new max width and height */ /* Note that these first two constants are used to define array sizes, * so they must be larger/equal to all other heights/widths. */ -#define GRAPH_HEIGHT 12 -#define GRAPH_WIDTH 77 +#define GRAPH_HEIGHT (12) +#define GRAPH_WIDTH (77) /* define graph height for split screen graphs */ -#define GRAPHSPLIT_HEIGHT 6 +#define GRAPHSPLIT_HEIGHT (6) #define GRAPHSPLIT_WIDTH GRAPH_WIDTH #define GRAPHCOMBINED_HEIGHT GRAPH_HEIGHT #define GRAPHCOMBINED_WIDTH GRAPH_WIDTH /* large split mode graph height */ -#define GRAPHLARGE_HEIGHT 11 +#define GRAPHLARGE_HEIGHT (11) /* rx is higher than need as we use it for the combined view */ int rx_graph[GRAPHSPLIT_WIDTH][GRAPHCOMBINED_HEIGHT]; diff --git a/src/hpux.c b/src/hpux.c index 76797cb..6d97f98 100644 --- a/src/hpux.c +++ b/src/hpux.c @@ -1,5 +1,5 @@ #include -#define WAIT_PCKS_COUNTER 15 +#define WAIT_PCKS_COUNTER (15) /* $Id: hpux.c,v 1.7 2003/02/23 17:26:02 hscholz Exp $ */ diff --git a/src/if_media.c b/src/if_media.c index 01ad732..1ce2069 100644 --- a/src/if_media.c +++ b/src/if_media.c @@ -14,15 +14,15 @@ #endif #if defined (__OpenBSD__) || defined (__NetBSD__) || defined (__MicroBSD__) || defined (__APPLE__) -#define NON_FreeBSD 1 -#define WIRELESS 1 +#define NON_FreeBSD (1) +#define WIRELESS (1) #else #if defined(__FreeBSD__) #if (__FreeBSD_version >= 450000) -#define WIRELESS 1 +#define WIRELESS (1) #endif #else -#define WIRELESS 0 +#define WIRELESS (0) #endif #endif diff --git a/src/if_media.h b/src/if_media.h index 8433dbf..ebe92f8 100644 --- a/src/if_media.h +++ b/src/if_media.h @@ -1,5 +1,5 @@ /* interface speed detection errors */ -#define ERR_IFACE_NO_SPEED -1 -#define ERR_IFACE_DOWN -2 +#define ERR_IFACE_NO_SPEED (-1) +#define ERR_IFACE_DOWN (-2) int get_if_speed(char *); diff --git a/theme.h b/theme.h index b123c0e..97904ff 100644 --- a/theme.h +++ b/theme.h @@ -31,45 +31,45 @@ typedef struct { theme t; /* possible entities */ -#define E_BACKGROUND 0x0101 -#define E_RX 0x0102 -#define E_TX 0x0104 -#define E_TEXT 0x0108 -#define E_TEXTVAR 0x0110 -#define E_TITLE 0x0120 -#define E_RXATTR 0x0201 -#define E_TXATTR 0x0202 -#define E_TEXTATTR 0x0204 -#define E_TEXTVARATTR 0x0208 -#define E_TITLEATTR 0x0210 +#define E_BACKGROUND (0x0101) +#define E_RX (0x0102) +#define E_TX (0x0104) +#define E_TEXT (0x0108) +#define E_TEXTVAR (0x0110) +#define E_TITLE (0x0120) +#define E_RXATTR (0x0201) +#define E_TXATTR (0x0202) +#define E_TEXTATTR (0x0204) +#define E_TEXTVARATTR (0x0208) +#define E_TITLEATTR (0x0210) #define E_ALL (E_BACKGROUND | E_RX | E_TX | E_TEXT | E_TEXTVAR | \ E_RXATTR | E_TXATTR | E_TEXTATTR | E_TEXTVARATTR | \ E_TITLE | E_TITLEATTR) -#define E_NULL 0 +#define E_NULL (0) /* color definitions */ #ifndef COLOR_BLACK -#define COLOR_BLACK 0 -#define COLOR_RED 1 -#define COLOR_GREEN 2 -#define COLOR_YELLOW 3 -#define COLOR_BLUE 4 -#define COLOR_MAGENTA 5 -#define COLOR_CYAN 6 -#define COLOR_WHITE 7 +#define COLOR_BLACK (0) +#define COLOR_RED (1) +#define COLOR_GREEN (2) +#define COLOR_YELLOW (3) +#define COLOR_BLUE (4) +#define COLOR_MAGENTA (5) +#define COLOR_CYAN (6) +#define COLOR_WHITE (7) #endif -#define COLOR_TRANSPARENT -1 +#define COLOR_TRANSPARENT (-1) -#define COL_NORMAL 0 -#define COL_BOLD 1 -#define COL_DIM 2 +#define COL_NORMAL (0) +#define COL_BOLD (1) +#define COL_DIM (2) /* internal color pairs */ -#define PAIR_TEXT 1 -#define PAIR_RX 2 -#define PAIR_TX 3 -#define PAIR_VAR 4 -#define PAIR_TITLE 5 +#define PAIR_TEXT (1) +#define PAIR_RX (2) +#define PAIR_TX (3) +#define PAIR_VAR (4) +#define PAIR_TITLE (5) #endif From 32e054ac9542a8f85e26f50e94c07b9cc754610a Mon Sep 17 00:00:00 2001 From: Iksas Date: Fri, 30 Aug 2024 21:25:15 +0200 Subject: [PATCH 6/9] fix calculation of maximum speed --- slurm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/slurm.c b/slurm.c index 10fe994..5a7b04a 100644 --- a/slurm.c +++ b/slurm.c @@ -379,7 +379,7 @@ int update_stat_large(void) /* max speed calculation has to be redone */ for (i = 0; i < GRAPH_WIDTH; i++) { if (rx_speedarray[i] > tmp_maxspeed) { - tmp_maxspeed = rx_speedarray[1]; + tmp_maxspeed = rx_speedarray[i]; tmp_maxspeedpos = i; } } @@ -394,7 +394,7 @@ int update_stat_large(void) /* max speed calculation has to be redone */ for (i = 0; i < GRAPH_WIDTH; i++) { if (tx_speedarray[i] > tmp_maxspeed) { - tmp_maxspeed = tx_speedarray[1]; + tmp_maxspeed = tx_speedarray[i]; tmp_maxspeedpos = i; } } @@ -665,7 +665,7 @@ int update_stat_split(void) /* max speed calculation has to be redone */ for (i = 0; i < GRAPHSPLIT_WIDTH; i++) { if (rx_speedarray[i] > tmp_maxspeed) { - tmp_maxspeed = rx_speedarray[1]; + tmp_maxspeed = rx_speedarray[i]; tmp_maxspeedpos = i; } } @@ -680,7 +680,7 @@ int update_stat_split(void) /* max speed calculation has to be redone */ for (i = 0; i < GRAPHSPLIT_WIDTH; i++) { if (tx_speedarray[i] > tmp_maxspeed) { - tmp_maxspeed = tx_speedarray[1]; + tmp_maxspeed = tx_speedarray[i]; tmp_maxspeedpos = i; } } @@ -922,7 +922,7 @@ int update_stat_combined(void) /* max speed calculation has to be redone */ for (i = 0; i < GRAPHCOMBINED_WIDTH; i++) { if (rx_speedarray[i] > tmp_maxspeed) { - tmp_maxspeed = rx_speedarray[1]; + tmp_maxspeed = rx_speedarray[i]; tmp_maxspeedpos = i; } } From c8c32a9ad743af5619dfcb1e7fb52b365e296540 Mon Sep 17 00:00:00 2001 From: Iksas Date: Fri, 30 Aug 2024 22:02:54 +0200 Subject: [PATCH 7/9] fix a bug where the graph would not automatically re-scale --- slurm.c | 276 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 143 insertions(+), 133 deletions(-) diff --git a/slurm.c b/slurm.c index 5a7b04a..72203e2 100644 --- a/slurm.c +++ b/slurm.c @@ -345,27 +345,50 @@ int update_stat_large(void) tx_overallmax = txspeed; } - /* - * update the Graph Top Speed field - * as it might be shorter than before just be sure to not leave - * trailing garbage by printing spaces + + /* prepare the graph array + * + * shift the graph to the left and then add the last entry + * in addition move the traffic stats left */ - if (rx_scalechanged) { - snprintf(draw, DRAWLEN - 1, "%.2f KB/s", - (float) rx_maxspeed / 1024); - strncat(draw, " ", DRAWLEN - strlen(draw)); - mvprintw(22, 24, "%s", draw); + /* move */ + for (x = GRAPH_WIDTH - 1; x >= 1; x--) { + rx_speedarray[x] = rx_speedarray[x - 1]; + tx_speedarray[x] = tx_speedarray[x - 1]; + for (y = 0; y < GRAPHLARGE_HEIGHT; y++) { + rx_graph[x][y] = rx_graph[x - 1][y]; + tx_graph[x][y] = tx_graph[x - 1][y]; + } } - if (tx_scalechanged) { - snprintf(draw, DRAWLEN - 1, "%.2f KB/s", - (float) tx_maxspeed / 1024); - strncat(draw, " ", DRAWLEN - strlen(draw)); - mvprintw(22, 65, "%s", draw); + + /* add the last column */ + if (rx_maxspeed > 0) + i = (int) ((rxspeed / rx_maxspeed) * GRAPHLARGE_HEIGHT); + for (y = 0; y <= GRAPHLARGE_HEIGHT; y++) { + if (i > y) + rx_graph[0][y] = 1; + else + rx_graph[0][y] = 0; } + if (0 != (int) rxspeed) + rx_graph[0][1] = 1; + rx_speedarray[0] = rxspeed; + if (tx_maxspeed > 0) + i = (int) ((txspeed / tx_maxspeed) * GRAPHLARGE_HEIGHT); + for (y = 0; y <= GRAPHLARGE_HEIGHT; y++) { + if (i > y) + tx_graph[0][y] = 1; + else + tx_graph[0][y] = 0; + } + if (0 != (int) txspeed) + tx_graph[0][1] = 1; + tx_speedarray[0] = txspeed; - /* increment position of max speed as we move the graph */ + + /* increment position of max speed as we moved the graph */ rx_maxspeedpos++; tx_maxspeedpos++; @@ -375,7 +398,7 @@ int update_stat_large(void) tmp_maxspeed = 0; tmp_maxspeedpos = 1; - if (rx_maxspeedpos >= GRAPH_WIDTH) { + if (rx_maxspeedpos >= (GRAPH_WIDTH-1)) { /* max speed calculation has to be redone */ for (i = 0; i < GRAPH_WIDTH; i++) { if (rx_speedarray[i] > tmp_maxspeed) { @@ -386,11 +409,12 @@ int update_stat_large(void) /* set new values */ rx_maxspeed = tmp_maxspeed; rx_maxspeedpos = tmp_maxspeedpos; + rx_scalechanged++; } tmp_maxspeed = 0; tmp_maxspeedpos = 1; - if (tx_maxspeedpos >= GRAPH_WIDTH) { + if (tx_maxspeedpos >= (GRAPH_WIDTH-1)) { /* max speed calculation has to be redone */ for (i = 0; i < GRAPH_WIDTH; i++) { if (tx_speedarray[i] > tmp_maxspeed) { @@ -401,48 +425,28 @@ int update_stat_large(void) /* set new values */ tx_maxspeed = tmp_maxspeed; tx_maxspeedpos = tmp_maxspeedpos; + tx_scalechanged++; } - /* prepare the graph array - * - * shift the graph to the left and then add the last entry - * in addition move the traffic stats left + /* + * update the Graph Top Speed field + * as it might be shorter than before just be sure to not leave + * trailing garbage by printing spaces */ - /* move */ - for (x = GRAPH_WIDTH - 1; x >= 1; x--) { - rx_speedarray[x] = rx_speedarray[x - 1]; - tx_speedarray[x] = tx_speedarray[x - 1]; - for (y = 0; y < GRAPHLARGE_HEIGHT; y++) { - rx_graph[x][y] = rx_graph[x - 1][y]; - tx_graph[x][y] = tx_graph[x - 1][y]; - } + if (rx_scalechanged) { + snprintf(draw, DRAWLEN - 1, "%.2f KB/s", + (float) rx_maxspeed / 1024); + strncat(draw, " ", DRAWLEN - strlen(draw)); + mvprintw(22, 24, "%s", draw); } - - /* add the last column */ - if (rx_maxspeed > 0) - i = (int) ((rxspeed / rx_maxspeed) * GRAPHLARGE_HEIGHT); - for (y = 0; y <= GRAPHLARGE_HEIGHT; y++) { - if (i > y) - rx_graph[0][y] = 1; - else - rx_graph[0][y] = 0; + if (tx_scalechanged) { + snprintf(draw, DRAWLEN - 1, "%.2f KB/s", + (float) tx_maxspeed / 1024); + strncat(draw, " ", DRAWLEN - strlen(draw)); + mvprintw(22, 65, "%s", draw); } - if (0 != (int) rxspeed) - rx_graph[0][1] = 1; - rx_speedarray[0] = rxspeed; - if (tx_maxspeed > 0) - i = (int) ((txspeed / tx_maxspeed) * GRAPHLARGE_HEIGHT); - for (y = 0; y <= GRAPHLARGE_HEIGHT; y++) { - if (i > y) - tx_graph[0][y] = 1; - else - tx_graph[0][y] = 0; - } - if (0 != (int) txspeed) - tx_graph[0][1] = 1; - tx_speedarray[0] = txspeed; /* * rescale graph @@ -633,25 +637,48 @@ int update_stat_split(void) } } - /* - * update the Graph Top Speed field - * as it might be shorter than before just be sure to not leave - * trailing garbage by printing spaces + /* prepare the graph array + * + * shift the graph to the left and then add the last entry + * in addition move the traffic stats left */ - if (rx_scalechanged) { - snprintf(draw, DRAWLEN - 1, "%.2f KB/s", rx_maxspeed / 1024); - strncat(draw, " ", DRAWLEN - strlen(draw)); - mvprintw(18, 24, "%s", draw); + /* move */ + for (x = GRAPHSPLIT_WIDTH - 1; x >= 1; x--) { + rx_speedarray[x] = rx_speedarray[x - 1]; + tx_speedarray[x] = tx_speedarray[x - 1]; + for (y = 0; y < GRAPHSPLIT_HEIGHT; y++) { + rx_graph[x][y] = rx_graph[x - 1][y]; + tx_graph[x][y] = tx_graph[x - 1][y]; + } } - if (tx_scalechanged) { - snprintf(draw, DRAWLEN - 1, "%.2f KB/s", tx_maxspeed / 1024); - strncat(draw, " ", DRAWLEN - strlen(draw)); - mvprintw(18, 65, "%s", draw); + + /* add the last column */ + if (rx_maxspeed > 0) + i = (int) ((rxspeed / rx_maxspeed) * GRAPHSPLIT_HEIGHT); + for (y = 0; y <= GRAPHSPLIT_HEIGHT; y++) { + if (i > y) + rx_graph[0][y] = 1; + else + rx_graph[0][y] = 0; } + if (0 != (int) rxspeed) + rx_graph[0][1] = 1; + rx_speedarray[0] = rxspeed; + if (tx_maxspeed > 0) + i = (int) ((txspeed / tx_maxspeed) * GRAPHSPLIT_HEIGHT); + for (y = 0; y <= GRAPHSPLIT_HEIGHT; y++) { + if (i > y) + tx_graph[0][y] = 1; + else + tx_graph[0][y] = 0; + } + if (0 != (int) txspeed) + tx_graph[0][1] = 1; + tx_speedarray[0] = txspeed; - /* increment position of max speed as we move the graph */ + /* increment position of max speed as we moved the graph */ rx_maxspeedpos++; tx_maxspeedpos++; @@ -672,6 +699,7 @@ int update_stat_split(void) /* set new values */ rx_maxspeed = tmp_maxspeed; rx_maxspeedpos = tmp_maxspeedpos; + rx_scalechanged++; } tmp_maxspeed = 0; @@ -687,48 +715,26 @@ int update_stat_split(void) /* set new values */ tx_maxspeed = tmp_maxspeed; tx_maxspeedpos = tmp_maxspeedpos; + tx_scalechanged++; } - /* prepare the graph array - * - * shift the graph to the left and then add the last entry - * in addition move the traffic stats left + /* + * update the Graph Top Speed field + * as it might be shorter than before just be sure to not leave + * trailing garbage by printing spaces */ - /* move */ - for (x = GRAPHSPLIT_WIDTH - 1; x >= 1; x--) { - rx_speedarray[x] = rx_speedarray[x - 1]; - tx_speedarray[x] = tx_speedarray[x - 1]; - for (y = 0; y < GRAPHSPLIT_HEIGHT; y++) { - rx_graph[x][y] = rx_graph[x - 1][y]; - tx_graph[x][y] = tx_graph[x - 1][y]; - } + if (rx_scalechanged) { + snprintf(draw, DRAWLEN - 1, "%.2f KB/s", rx_maxspeed / 1024); + strncat(draw, " ", DRAWLEN - strlen(draw)); + mvprintw(18, 24, "%s", draw); } - - /* add the last column */ - if (rx_maxspeed > 0) - i = (int) ((rxspeed / rx_maxspeed) * GRAPHSPLIT_HEIGHT); - for (y = 0; y <= GRAPHSPLIT_HEIGHT; y++) { - if (i > y) - rx_graph[0][y] = 1; - else - rx_graph[0][y] = 0; + if (tx_scalechanged) { + snprintf(draw, DRAWLEN - 1, "%.2f KB/s", tx_maxspeed / 1024); + strncat(draw, " ", DRAWLEN - strlen(draw)); + mvprintw(18, 65, "%s", draw); } - if (0 != (int) rxspeed) - rx_graph[0][1] = 1; - rx_speedarray[0] = rxspeed; - if (tx_maxspeed > 0) - i = (int) ((txspeed / tx_maxspeed) * GRAPHSPLIT_HEIGHT); - for (y = 0; y <= GRAPHSPLIT_HEIGHT; y++) { - if (i > y) - tx_graph[0][y] = 1; - else - tx_graph[0][y] = 0; - } - if (0 != (int) txspeed) - tx_graph[0][1] = 1; - tx_speedarray[0] = txspeed; /* * rescale graph @@ -897,39 +903,6 @@ int update_stat_combined(void) } } - /* - * update the Graph Top Speed field - * as it might be shorter than before just be sure to not leave - * trailing garbage by printing spaces - */ - - if (rx_scalechanged) { - snprintf(draw, DRAWLEN - 1, "%.2f KB/s", comb_maxspeed / 1024); - strncat(draw, " ", DRAWLEN - strlen(draw)); - mvprintw(19, 24, "%s", draw); - } - - /* increment position of max speed as we move the graph */ - rx_maxspeedpos++; - - /* check if max speed has to be lowered for the graph as the max speed - * was reached too long ago - */ - - tmp_maxspeed = 0; - tmp_maxspeedpos = 1; - if (rx_maxspeedpos >= GRAPHCOMBINED_WIDTH) { - /* max speed calculation has to be redone */ - for (i = 0; i < GRAPHCOMBINED_WIDTH; i++) { - if (rx_speedarray[i] > tmp_maxspeed) { - tmp_maxspeed = rx_speedarray[i]; - tmp_maxspeedpos = i; - } - } - /* set new values */ - comb_maxspeed = tmp_maxspeed; - rx_maxspeedpos = tmp_maxspeedpos; - } /* prepare the graph array * @@ -966,6 +939,43 @@ int update_stat_combined(void) rx_speedarray[0] = rxspeed; tx_speedarray[0] = txspeed; + + /* increment position of max speed as we moved the graph */ + rx_maxspeedpos++; + + /* check if max speed has to be lowered for the graph as the max speed + * was reached too long ago + */ + + tmp_maxspeed = 0; + tmp_maxspeedpos = 1; + if (rx_maxspeedpos >= (GRAPHCOMBINED_WIDTH-1)) { + /* max speed calculation has to be redone */ + for (i = 0; i < GRAPHCOMBINED_WIDTH; i++) { + if (rx_speedarray[i] > tmp_maxspeed) { + tmp_maxspeed = rx_speedarray[i]; + tmp_maxspeedpos = i; + } + } + /* set new values */ + comb_maxspeed = tmp_maxspeed; + rx_maxspeedpos = tmp_maxspeedpos; + rx_scalechanged++; + } + + /* + * update the Graph Top Speed field + * as it might be shorter than before just be sure to not leave + * trailing garbage by printing spaces + */ + + if (rx_scalechanged) { + snprintf(draw, DRAWLEN - 1, "%.2f KB/s", comb_maxspeed / 1024); + strncat(draw, " ", DRAWLEN - strlen(draw)); + mvprintw(19, 24, "%s", draw); + } + + /* * rescale graph * From c2022b3bb31f676df918ccceec154e061996124f Mon Sep 17 00:00:00 2001 From: Iksas Date: Fri, 30 Aug 2024 22:06:18 +0200 Subject: [PATCH 8/9] make sure graphs are centered when a terminal of width 80 is used --- slurm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slurm.h b/slurm.h index 9ad2ecd..dc040e9 100644 --- a/slurm.h +++ b/slurm.h @@ -162,7 +162,7 @@ static FILE *proc_net_dev; /* Note that these first two constants are used to define array sizes, * so they must be larger/equal to all other heights/widths. */ #define GRAPH_HEIGHT (12) -#define GRAPH_WIDTH (77) +#define GRAPH_WIDTH (78) /* define graph height for split screen graphs */ #define GRAPHSPLIT_HEIGHT (6) From 362f597e6fb7bdf13865d3320c3cd58b4bfc488d Mon Sep 17 00:00:00 2001 From: Iksas Date: Sat, 31 Aug 2024 14:04:21 +0200 Subject: [PATCH 9/9] fix more out-of-bounds memory accesses --- slurm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slurm.c b/slurm.c index 72203e2..2a8718a 100644 --- a/slurm.c +++ b/slurm.c @@ -495,7 +495,7 @@ int update_stat_large(void) */ for (y = GRAPHLARGE_HEIGHT - 1; y > 0; y--) { - for (x = 0; x <= GRAPH_WIDTH; x++) { + for (x = 0; x < GRAPH_WIDTH; x++) { /* RX graph */ if (rx_graph[x][y] == 1) { attrset(COLOR_PAIR(PAIR_RX) | @@ -783,7 +783,7 @@ int update_stat_split(void) */ for (y = GRAPHSPLIT_HEIGHT - 1; y > 0; y--) { - for (x = 0; x <= GRAPHSPLIT_WIDTH; x++) { + for (x = 0; x < GRAPHSPLIT_WIDTH; x++) { /* RX graph */ if (rx_graph[x][y] == 1) { attrset(COLOR_PAIR(PAIR_RX) | @@ -1007,7 +1007,7 @@ int update_stat_combined(void) */ for (y = GRAPHCOMBINED_HEIGHT - 1; y > 0; y--) { - for (x = 0; x <= GRAPHCOMBINED_WIDTH; x++) { + for (x = 0; x < GRAPHCOMBINED_WIDTH; x++) { /* RX graph */ if (rx_graph[x][y] == 1) { attrset(COLOR_PAIR(PAIR_RX) |