Permalink
Browse files

pass correct length to memchr, because it does not stop at '\0'. adju…

…st read data length, because become large value after adding separator length.
  • Loading branch information...
Watson1978 committed Feb 8, 2012
1 parent 5b50fb0 commit 08bc1749805141ae0de29a14f63047389b8c644f
Showing with 12 additions and 1 deletion.
  1. +12 −1 io.c
View
13 io.c
@@ -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).

0 comments on commit 08bc174

Please sign in to comment.