[Raw Msg Headers][Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

.forward files w/o newlines cause router memory leak (+fix)




A local emacs fan set up his .forward file without a newline.
After a few days the router process on that machine consumed
all available memory and hung.

I traced the problem to lib/linebuffer.c.  The function getline()
attempts to read a line of arbitrary length.  If the file has
no newline, the internal buffer used by getline doubles in size.

This patch grows the buffer only when it's full.

Cheers,

	Ken Lalonde, Dept. of Computer Science, University of Toronto


*** linebuffer.c.old	Thu Feb 10 10:13:39 1994
--- linebuffer.c	Fri Feb 11 07:15:37 1994
***************
*** 60,66 ****
  	/* assert block != NULL */
  	if (blen <= 0 || bend >= block + bsize) {
  		blen = fread((char *)block, 1, (int)bsize, fp);
! 		if (blen <= 0)
  			return 0;
  		bend = block;
  	}
--- 60,66 ----
  	/* assert block != NULL */
  	if (blen <= 0 || bend >= block + bsize) {
  		blen = fread((char *)block, 1, (int)bsize, fp);
! 		if (blen == 0)
  			return 0;
  		bend = block;
  	}
***************
*** 77,86 ****
  		if (bend > block)
  			bcopy((char *)bend, (char *)block, (int)blen);
  		/* get some more bytes which will hopefully contain a newline */
! 		bsize *= 2;
! 		block = (u_char *)erealloc((char *)block, bsize);
! 		n = fread((char *)(block+blen), 1, (int)bsize/2, fp);
! 		if (n <= 0) {
  			/* the file doesn't terminate in a newline */
  			n = blen;
  			blen = 0;
--- 77,90 ----
  		if (bend > block)
  			bcopy((char *)bend, (char *)block, (int)blen);
  		/* get some more bytes which will hopefully contain a newline */
! 		n = bsize - blen;
! 		if (n <= 0) {		/* grow the buffer */
! 			n = bsize;
! 			bsize *= 2;
! 			block = (u_char *)erealloc((char *)block, bsize);
! 		}
! 		n = fread((char *)(block+blen), 1, n, fp);
! 		if (n == 0) {
  			/* the file doesn't terminate in a newline */
  			n = blen;
  			blen = 0;