diff options
author | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-09-11 19:26:49 +0000 |
---|---|---|
committer | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-09-11 19:26:49 +0000 |
commit | f8edd2d8b4fef01135ffb89df166826520e8ccc9 (patch) | |
tree | 638d7cf7df4c1fc62a1ed00ba6181e08f427839e /src/buffer.c | |
parent | 5dd98ac658620e1d27df3dbde55ad34ba869028d (diff) | |
download | tinyproxy-f8edd2d8b4fef01135ffb89df166826520e8ccc9.tar.gz tinyproxy-f8edd2d8b4fef01135ffb89df166826520e8ccc9.zip |
Switched the memory allocation for the large strings to heap based instead
of stack based.
Diffstat (limited to '')
-rw-r--r-- | src/buffer.c | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/buffer.c b/src/buffer.c index 126f910..c6daa10 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1,4 +1,4 @@ -/* $Id: buffer.c,v 1.8 2001-09-11 04:38:23 rjkaes Exp $ +/* $Id: buffer.c,v 1.9 2001-09-11 19:26:49 rjkaes Exp $ * * The buffer used in each connection is a linked list of lines. As the lines * are read in and written out the buffer expands and contracts. Basically, @@ -185,8 +185,8 @@ static struct bufline_s *remove_from_buffer(struct buffer_s *buffptr) ssize_t readbuff(int fd, struct buffer_s *buffptr) { ssize_t bytesin; - unsigned char inbuf[MAXBUFFSIZE]; unsigned char *buffer; + unsigned char *newbuffer; assert(fd >= 0); assert(buffptr != NULL); @@ -194,22 +194,30 @@ ssize_t readbuff(int fd, struct buffer_s *buffptr) if (buffer_size(buffptr) >= MAXBUFFSIZE) return 0; - bytesin = read(fd, inbuf, MAXBUFFSIZE - buffer_size(buffptr)); + buffer = safemalloc(MAXBUFFSIZE); + if (!buffer) { + log_message(LOG_ERR, "Could not allocate memory in 'readbuff'"); + return 0; + } + + bytesin = read(fd, buffer, MAXBUFFSIZE - buffer_size(buffptr) - 1); if (bytesin > 0) { - if (!(buffer = safemalloc(bytesin))) { - log_message(LOG_ERR, "Could not allocate memory in 'readbuff'"); + newbuffer = saferealloc(buffer, bytesin); + if (!newbuffer) { + log_message(LOG_ERR, "Could not reallocate memory in 'readbuff'"); + safefree(buffer); return 0; } - memcpy(buffer, inbuf, bytesin); - if (add_to_buffer(buffptr, buffer, bytesin) < 0) { + if (add_to_buffer(buffptr, newbuffer, bytesin) < 0) { return -1; } return bytesin; } else if (bytesin == 0) { /* connection was closed by client */ + safefree(buffer); return -1; } else { switch (errno) { @@ -221,10 +229,12 @@ ssize_t readbuff(int fd, struct buffer_s *buffptr) # endif #endif case EINTR: + safefree(buffer); return 0; default: log_message(LOG_ERR, "recv error (%s) in 'readbuff'.", strerror(errno)); + safefree(buffer); return -1; } } |