Fix bug where read cursor would point to the wrong location in memory

This commit is contained in:
Nick Krichevsky 2018-09-02 19:16:20 -04:00
parent 475b162389
commit a958916a41

View file

@ -142,9 +142,9 @@ enum socket_read_result get_all_remote_parts(int socket_fd, struct http_message
ssize_t current_result_size = 0;
char *result = NULL;
char *buffer = malloc(BUFFER_SIZE * sizeof(char));
char *read_cursor;
ssize_t bytes_read;
int write_offset = 0;
int read_offset = 0;
bool have_start_line = false;
bool have_blank_line = false;
// Loop through all available info until we hit the end of the headers.
@ -153,16 +153,15 @@ enum socket_read_result get_all_remote_parts(int socket_fd, struct http_message
//Allocate a new result buffer if we need to, otherwise grow the existing one.
if (result == NULL) {
result = malloc(bytes_read);
read_cursor = result;
} else {
result = realloc(result, current_result_size);
}
memcpy(result + write_offset, buffer, bytes_read);
struct line_read_result line_result;
memset(&line_result, 0, sizeof(struct line_read_result));
while ((line_result = read_line(read_cursor, current_result_size, message),
while ((line_result = read_line(result + read_offset, current_result_size, message),
line_result.line_type != RESULT_NO_LINE)) {
read_cursor += line_result.bytes_read;
read_offset += line_result.bytes_read;
free(line_result.line);
if (!have_start_line && line_result.line_type == RESULT_BLANK_LINE) {
// If we don't have a start line, we can skip any blank lines.
@ -187,15 +186,14 @@ enum socket_read_result get_all_remote_parts(int socket_fd, struct http_message
have_start_line = true;
}
}
write_offset += current_result_size;
write_offset = current_result_size;
}
struct socket_read_info read_info = get_read_info(message->headers);
char *body_result;
if (read_info.strategy == STRATEGY_CHUNKED) {
// TODO: Implement
} else if (read_info.strategy == STRATEGY_CONTENT_LENGTH) {
int header_length = read_cursor - result;
int net_content_length = read_info.length - (current_result_size - header_length);
int net_content_length = read_info.length - (current_result_size - read_offset);
// Include space for null term
current_result_size += net_content_length + 1;
result = realloc(result, current_result_size);