Skip to content

Instantly share code, notes, and snippets.

@presuku
Created July 26, 2016 07:33
Show Gist options
  • Save presuku/ed0fb5104b76a0f23c9249141d521faa to your computer and use it in GitHub Desktop.
Save presuku/ed0fb5104b76a0f23c9249141d521faa to your computer and use it in GitHub Desktop.
highway issue 25 patch
diff --git a/src/search.c b/src/search.c
index 6248612..009b15d 100644
--- a/src/search.c
+++ b/src/search.c
@@ -69,8 +69,16 @@ char *grow_buf_if_shortage(size_t *cur_buf_size,
char *new_buf;
if (*cur_buf_size < need_size + buf_offset + NMAX) {
*cur_buf_size += need_size + (NMAX - need_size % NMAX);
+ ptrdiff_t copy_buf_size;
+ size_t size;
new_buf = (char *)hw_calloc(*cur_buf_size, SIZE_OF_CHAR);
- memcpy(new_buf, copy_buf, need_size);
+ copy_buf_size = copy_buf - current_buf;
+ if (need_size < copy_buf_size) {
+ size = need_size;
+ } else {
+ size = copy_buf_size;
+ }
+ memcpy(new_buf, copy_buf, size);
tc_free(current_buf);
} else {
new_buf = copy_buf;
@@ -418,7 +426,7 @@ int search(int fd,
// Search end position of the last line in the buffer. We search from the first position
// and end position of the last line.
size_t search_len;
- if (read_len < NMAX) {
+ if (read_len < NMAX && read_sum < n) {
last_line_end = buf + read_sum;
search_len = read_sum;
buf[read_sum] = eol;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment