wmifs: Add BUFFER_SIZE constant.

Patch by Stephen Pitts <smpitts@midsouth.rr.com>.  First appeared in Debian
package version 1.3b1-4.

From https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=41746:

wmifs started crashing on startup on my system,
so I grabbed the source, added -g to the
Makefile, and ran it through gdb.
(Wow, the wonders of Open Source!!).

I found out that it has the hard-coded assumption
that a line in  /proc/net/dev will be no longer
than 128 bytes. Since my ethernet card has over
1 GB in traffic , my eth0 line was 129 bytes long.
I added a new constant, BUFFER_SIZE, that
determines the size of the buffer used for fgets.

Right now, its at 512 bytes, so that gives it
a large margin of error, until we have petabyte
Ethernet! The patch is attached.
This commit is contained in:
Doug Torrance 2014-10-22 16:34:29 -05:00 committed by Carlos R. Mafra
parent 3e9e54c1e4
commit 3bd48a06c5

View file

@ -74,6 +74,11 @@
---- ----
Changes: Changes:
--- ---
07/21/1999 (Stephen Pitts, smpitts@midsouth.rr.com)
* Added new constant: BUFFER_SIZE to determine the size
of the buffer used in fgets() operations. Right now,
its at 512 bytes. Fixed crashing on my system when
one line of /proc/net/dev was longer than 128 bytes
04/05/1998 (Martijn Pieterse, pieterse@xs4all.nl) 04/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
* Changed the "middle of the waveform" line color * Changed the "middle of the waveform" line color
* Moved the RedrawWindow out of the main loop. * Moved the RedrawWindow out of the main loop.
@ -197,6 +202,8 @@
#define WMIFS_VERSION "1.2.1" #define WMIFS_VERSION "1.2.1"
/* the size of the buffer read from /proc/net/* */
#define BUFFER_SIZE 512
/**********************/ /**********************/
/* External Variables */ /* External Variables */
/**********************/ /**********************/
@ -340,7 +347,7 @@ void wmifs_routine(int argc, char **argv) {
long ipacket, opacket, istat, ostat; long ipacket, opacket, istat, ostat;
char temp[128]; char temp[BUFFER_SIZE];
char *p; char *p;
for (i=0; i<MAX_STAT_DEVICES; i++) { for (i=0; i<MAX_STAT_DEVICES; i++) {
@ -562,7 +569,7 @@ void DrawActiveIFS(char *name) {
int get_statistics(char *devname, long *ip, long *op, long *is, long *os) { int get_statistics(char *devname, long *ip, long *op, long *is, long *os) {
FILE *fp; FILE *fp;
char temp[128]; char temp[BUFFER_SIZE];
char *p; char *p;
char *tokens = " |:\n"; char *tokens = " |:\n";
int input, output; int input, output;
@ -596,8 +603,8 @@ int get_statistics(char *devname, long *ip, long *op, long *is, long *os) {
/* Read from /proc/net/dev the stats! */ /* Read from /proc/net/dev the stats! */
fp = fopen("/proc/net/dev", "r"); fp = fopen("/proc/net/dev", "r");
fgets(temp, 128, fp); fgets(temp, BUFFER_SIZE, fp);
fgets(temp, 128, fp); fgets(temp, BUFFER_SIZE, fp);
input = -1; input = -1;
output = -1; output = -1;
@ -614,7 +621,7 @@ int get_statistics(char *devname, long *ip, long *op, long *is, long *os) {
p = strtok(NULL, tokens); p = strtok(NULL, tokens);
} while (input == -1 || output == -1); } while (input == -1 || output == -1);
while (fgets(temp, 128, fp)) { while (fgets(temp, BUFFER_SIZE, fp)) {
if (strstr(temp, devname)) { if (strstr(temp, devname)) {
found = 0; found = 0;
p = strtok(temp, tokens); p = strtok(temp, tokens);
@ -645,15 +652,16 @@ int get_statistics(char *devname, long *ip, long *op, long *is, long *os) {
int stillonline(char *ifs) { int stillonline(char *ifs) {
FILE *fp; FILE *fp;
char temp[128]; char temp[BUFFER_SIZE];
int i; int i;
i = 0; i = 0;
fp = fopen("/proc/net/route", "r"); fp = fopen("/proc/net/route", "r");
if (fp) { if (fp) {
while (fgets(temp, 128, fp)) { while (fgets(temp, BUFFER_SIZE, fp)) {
if (strstr(temp, ifs)) { if (strstr(temp, ifs)) {
i = 1; /* Line is alive */ i = 1; /* Line is alive */
break;
} }
} }
fclose(fp); fclose(fp);
@ -668,7 +676,7 @@ int stillonline(char *ifs) {
int checknetdevs(void) { int checknetdevs(void) {
FILE *fd; FILE *fd;
char temp[128]; char temp[BUFFER_SIZE];
char *p; char *p;
int i=0,j; int i=0,j;
int k; int k;
@ -685,11 +693,14 @@ int checknetdevs(void) {
fd = fopen("/proc/net/dev", "r"); fd = fopen("/proc/net/dev", "r");
if (fd) { if (fd) {
/* Skip the first 2 lines */ /* Skip the first 2 lines */
fgets(temp, 128, fd); fgets(temp, BUFFER_SIZE, fd);
fgets(temp, 128, fd); fgets(temp, BUFFER_SIZE, fd);
while (fgets(temp, 128, fd)) { while (fgets(temp, BUFFER_SIZE, fd)) {
p = strtok(temp, tokens); p = strtok(temp, tokens);
if(p == NULL) {
printf("Barfed on: %s", temp);
break;
}
/* Skip dummy code */ /* Skip dummy code */
if (!strncmp(p, "dummy", 5)) if (!strncmp(p, "dummy", 5))