diff --git a/src/line_buffer.cc b/src/line_buffer.cc index 3362b7ab..90552d55 100644 --- a/src/line_buffer.cc +++ b/src/line_buffer.cc @@ -222,23 +222,24 @@ throw (error) void line_buffer::resize_buffer(size_t new_max) throw (error) { - char *tmp, *old; - require(this->lb_bz_file || this->lb_gz_file || new_max <= MAX_LINE_BUFFER_SIZE); - /* Still need more space, try a realloc. */ - old = this->lb_buffer.release(); - this->lb_share_manager.invalidate_refs(); - tmp = (char *)realloc(old, new_max); - if (tmp != NULL) { - this->lb_buffer = tmp; - this->lb_buffer_max = new_max; - } - else { - this->lb_buffer = old; + if (new_max > this->lb_buffer_max) { + char *tmp, *old; - throw error(ENOMEM); + /* Still need more space, try a realloc. */ + old = this->lb_buffer.release(); + this->lb_share_manager.invalidate_refs(); + tmp = (char *) realloc(old, new_max); + if (tmp != NULL) { + this->lb_buffer = tmp; + this->lb_buffer_max = new_max; + } else { + this->lb_buffer = old; + + throw error(ENOMEM); + } } } @@ -249,6 +250,12 @@ throw (error) require(max_length <= MAX_LINE_BUFFER_SIZE); + if (this->lb_file_size != -1) { + if (start + max_length > this->lb_file_size) { + max_length = (this->lb_file_size - start); + } + } + /* * Check to see if the start is inside the cached range or immediately * after. diff --git a/src/line_buffer.hh b/src/line_buffer.hh index 3677ff61..d2d6997f 100644 --- a/src/line_buffer.hh +++ b/src/line_buffer.hh @@ -230,6 +230,7 @@ private: char *retval; require(buffer_offset >= 0); + require(this->lb_buffer_size >= buffer_offset); retval = &this->lb_buffer[buffer_offset]; avail_out = this->lb_buffer_size - buffer_offset;