summaryrefslogtreecommitdiff
path: root/ext/stringio/stringio.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/stringio/stringio.c')
-rw-r--r--ext/stringio/stringio.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 2d0229accaa..88f6be42d34 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -857,10 +857,13 @@ strio_read(argc, argv, self)
/* fall through */
case 0:
olen = -1;
- len = RSTRING(ptr->string)->len - ptr->pos;
- if (len == 0 && ptr->pos == RSTRING(ptr->string)->len) {
+ len = RSTRING(ptr->string)->len;
+ if (len <= ptr->pos) {
if (ptr->flags & STRIO_EOF) return Qnil;
- ptr->flags |= STRIO_EOF;
+ len = 0;
+ }
+ else {
+ len -= ptr->pos;
}
break;
default:
@@ -869,11 +872,12 @@ strio_read(argc, argv, self)
str = rb_str_substr(ptr->string, ptr->pos, len);
if (NIL_P(str)) {
if (!(ptr->flags & STRIO_EOF)) str = rb_str_new(0, 0);
- if (olen) ptr->flags |= STRIO_EOF;
+ len = 0;
}
else {
- ptr->pos += RSTRING(str)->len;
+ ptr->pos += len = RSTRING(str)->len;
}
+ if (olen < 0 || olen > len) ptr->flags |= STRIO_EOF;
return str;
}