Switched the memory allocation for the large strings to heap based instead

of stack based.
This commit is contained in:
Robert James Kaes 2001-09-11 19:26:49 +00:00
parent 5dd98ac658
commit f8edd2d8b4
2 changed files with 59 additions and 13 deletions

View File

@ -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 * 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, * 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 readbuff(int fd, struct buffer_s *buffptr)
{ {
ssize_t bytesin; ssize_t bytesin;
unsigned char inbuf[MAXBUFFSIZE];
unsigned char *buffer; unsigned char *buffer;
unsigned char *newbuffer;
assert(fd >= 0); assert(fd >= 0);
assert(buffptr != NULL); assert(buffptr != NULL);
@ -194,22 +194,30 @@ ssize_t readbuff(int fd, struct buffer_s *buffptr)
if (buffer_size(buffptr) >= MAXBUFFSIZE) if (buffer_size(buffptr) >= MAXBUFFSIZE)
return 0; 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 (bytesin > 0) {
if (!(buffer = safemalloc(bytesin))) { newbuffer = saferealloc(buffer, bytesin);
log_message(LOG_ERR, "Could not allocate memory in 'readbuff'"); if (!newbuffer) {
log_message(LOG_ERR, "Could not reallocate memory in 'readbuff'");
safefree(buffer);
return 0; return 0;
} }
memcpy(buffer, inbuf, bytesin); if (add_to_buffer(buffptr, newbuffer, bytesin) < 0) {
if (add_to_buffer(buffptr, buffer, bytesin) < 0) {
return -1; return -1;
} }
return bytesin; return bytesin;
} else if (bytesin == 0) { } else if (bytesin == 0) {
/* connection was closed by client */ /* connection was closed by client */
safefree(buffer);
return -1; return -1;
} else { } else {
switch (errno) { switch (errno) {
@ -221,10 +229,12 @@ ssize_t readbuff(int fd, struct buffer_s *buffptr)
# endif # endif
#endif #endif
case EINTR: case EINTR:
safefree(buffer);
return 0; return 0;
default: default:
log_message(LOG_ERR, "recv error (%s) in 'readbuff'.", log_message(LOG_ERR, "recv error (%s) in 'readbuff'.",
strerror(errno)); strerror(errno));
safefree(buffer);
return -1; return -1;
} }
} }

View File

@ -1,4 +1,4 @@
/* $Id: reqs.c,v 1.21 2001-09-08 18:58:37 rjkaes Exp $ /* $Id: reqs.c,v 1.22 2001-09-11 19:26:49 rjkaes Exp $
* *
* This is where all the work in tinyproxy is actually done. Incoming * This is where all the work in tinyproxy is actually done. Incoming
* connections have a new thread created for them. The thread then * connections have a new thread created for them. The thread then
@ -110,7 +110,7 @@ static inline void trim(char *string, unsigned int len)
static int process_method(struct conn_s *connptr) static int process_method(struct conn_s *connptr)
{ {
URI *uri = NULL; URI *uri = NULL;
char inbuf[LINE_LENGTH]; char *inbuf;
char *buffer = NULL, *request = NULL, *port = NULL; char *buffer = NULL, *request = NULL, *port = NULL;
regex_t preg; regex_t preg;
@ -124,10 +124,17 @@ static int process_method(struct conn_s *connptr)
getpeer_ip(connptr->client_fd, peer_ipaddr); getpeer_ip(connptr->client_fd, peer_ipaddr);
inbuf = safemalloc(LINE_LENGTH);
if (!inbuf) {
log_message(LOG_ERR, "Could not allocate memory in 'process_method'.");
return -2;
}
len = readline(connptr->client_fd, inbuf, LINE_LENGTH); len = readline(connptr->client_fd, inbuf, LINE_LENGTH);
if (len <= 0) { if (len <= 0) {
log_message(LOG_ERR, "Client [%s] closed socket before read.", peer_ipaddr); log_message(LOG_ERR, "Client [%s] closed socket before read.", peer_ipaddr);
update_stats(STAT_BADCONN); update_stats(STAT_BADCONN);
safefree(inbuf);
return -2; return -2;
} }
@ -308,11 +315,13 @@ static int process_method(struct conn_s *connptr)
if (safe_write(connptr->server_fd, "Connection: close\r\n", 19) < 0) if (safe_write(connptr->server_fd, "Connection: close\r\n", 19) < 0)
goto COMMON_EXIT; goto COMMON_EXIT;
safefree(inbuf);
safefree(request); safefree(request);
free_uri(uri); free_uri(uri);
return 0; return 0;
COMMON_EXIT: COMMON_EXIT:
safefree(inbuf);
free_uri(uri); free_uri(uri);
EARLY_EXIT: EARLY_EXIT:
@ -354,18 +363,24 @@ static int compare_header(char *line)
*/ */
static int pull_client_data(struct conn_s *connptr, unsigned long int length) static int pull_client_data(struct conn_s *connptr, unsigned long int length)
{ {
char buffer[MAXBUFFSIZE]; char *buffer;
ssize_t len; ssize_t len;
buffer = safemalloc(MAXBUFFSIZE);
if (!buffer)
return -1;
do { do {
len = safe_read(connptr->client_fd, buffer, min(MAXBUFFSIZE, length)); len = safe_read(connptr->client_fd, buffer, min(MAXBUFFSIZE, length));
if (len <= 0) { if (len <= 0) {
safefree(buffer);
return -1; return -1;
} }
if (!connptr->output_message) { if (!connptr->output_message) {
if (safe_write(connptr->server_fd, buffer, len) < 0) { if (safe_write(connptr->server_fd, buffer, len) < 0) {
safefree(buffer);
return -1; return -1;
} }
} }
@ -373,6 +388,7 @@ static int pull_client_data(struct conn_s *connptr, unsigned long int length)
length -= len; length -= len;
} while (length > 0); } while (length > 0);
safefree(buffer);
return 0; return 0;
} }
@ -406,7 +422,7 @@ static int add_xtinyproxy_header(struct conn_s *connptr)
*/ */
static int process_client_headers(struct conn_s *connptr) static int process_client_headers(struct conn_s *connptr)
{ {
char header[LINE_LENGTH]; char *header;
long content_length = -1; long content_length = -1;
char *skipheaders[] = { char *skipheaders[] = {
@ -416,8 +432,13 @@ static int process_client_headers(struct conn_s *connptr)
}; };
int i; int i;
header = safemalloc(LINE_LENGTH);
if (!header)
return -1;
for ( ; ; ) { for ( ; ; ) {
if (readline(connptr->client_fd, header, LINE_LENGTH) < 0) { if (readline(connptr->client_fd, header, LINE_LENGTH) < 0) {
safefree(header);
return -1; return -1;
} }
@ -449,23 +470,29 @@ static int process_client_headers(struct conn_s *connptr)
content_length = atol(content_ptr); content_length = atol(content_ptr);
} }
if (safe_write(connptr->server_fd, header, strlen(header)) < 0) if (safe_write(connptr->server_fd, header, strlen(header)) < 0) {
safefree(header);
return -1; return -1;
}
} }
if (!connptr->output_message) { if (!connptr->output_message) {
#ifdef XTINYPROXY_ENABLE #ifdef XTINYPROXY_ENABLE
if (config.my_domain if (config.my_domain
&& add_xtinyproxy_header(connptr) < 0) { && add_xtinyproxy_header(connptr) < 0) {
safefree(header);
return -1; return -1;
} }
#endif /* XTINYPROXY */ #endif /* XTINYPROXY */
if (safe_write(connptr->server_fd, header, strlen(header)) < 0) { if (safe_write(connptr->server_fd, header, strlen(header)) < 0) {
safefree(header);
return -1; return -1;
} }
} }
safefree(header);
/* /*
* Spin here pulling the data from the client. * Spin here pulling the data from the client.
*/ */
@ -481,10 +508,15 @@ static int process_client_headers(struct conn_s *connptr)
*/ */
static int process_server_headers(struct conn_s *connptr) static int process_server_headers(struct conn_s *connptr)
{ {
char header[LINE_LENGTH]; char *header;
header = safemalloc(LINE_LENGTH);
if (!header)
return -1;
for ( ; ; ) { for ( ; ; ) {
if (readline(connptr->server_fd, header, LINE_LENGTH) < 0) { if (readline(connptr->server_fd, header, LINE_LENGTH) < 0) {
safefree(header);
return -1; return -1;
} }
@ -495,14 +527,18 @@ static int process_server_headers(struct conn_s *connptr)
if (!connptr->simple_req if (!connptr->simple_req
&& safe_write(connptr->client_fd, header, strlen(header)) < 0) { && safe_write(connptr->client_fd, header, strlen(header)) < 0) {
safefree(header);
return -1; return -1;
} }
} }
if (!connptr->simple_req if (!connptr->simple_req
&& safe_write(connptr->client_fd, header, strlen(header)) < 0) { && safe_write(connptr->client_fd, header, strlen(header)) < 0) {
safefree(header);
return -1; return -1;
} }
safefree(header);
return 0; return 0;
} }