Skip to content

Commit

Permalink
pass correct length to memchr, because it does not stop at '\0'. adju…
Browse files Browse the repository at this point in the history
…st read data length, because become large value after adding separator length.
  • Loading branch information
Watson1978 committed Feb 8, 2012
1 parent 5b50fb0 commit 08bc174
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion io.c
Expand Up @@ -1599,25 +1599,36 @@ rb_io_getline_1(VALUE sep, long line_limit, VALUE io)
- io_struct->buf_offset;
const UInt8 *pos = cache;
long data_read = 0;
#define REST_CACHE_LENGTH (cache_len - (long)(pos - cache))

while (true) {
const UInt8 *tmp = memchr(pos, sepstr[0], cache_len);
const UInt8 *tmp = memchr(pos, sepstr[0], REST_CACHE_LENGTH);
if (tmp == NULL) {
data_read = cache_len;
break;
}
if (seplen == 1
|| memcmp(tmp + 1, &sepstr[1], seplen - 1) == 0) {
data_read = tmp - cache + seplen;
if (data_read > cache_len) {
data_read = cache_len;
}
break;
}
pos = tmp + seplen;
if (REST_CACHE_LENGTH <= 0) {
data_read = cache_len;
break;
}
}
if (data_read == 0) {
return Qnil;
}

rb_bstr_concat(bstr, cache, data_read);
rb_io_read_update(io_struct, data_read);

#undef REST_CACHE_LENGTH
}
else {
// Read from IO (slow).
Expand Down

0 comments on commit 08bc174

Please sign in to comment.