summaryrefslogtreecommitdiff
path: root/ext/stringio
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-03-07 22:39:38 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-03-08 10:13:29 +0900
commit32a13591e0bb6e96b05452e214f14eda21ee3aa9 (patch)
tree27515ac48fffd1e064b484e476ed90a982ef5ef2 /ext/stringio
parent99f54c08953a96ebaa822f4fdce6d9de47f99814 (diff)
[ruby/stringio] Check if closed in loop
[Bug #17675] https://bugs.ruby-lang.org/issues/17675 https://github.com/ruby/stringio/commit/1ed61d0cbc
Diffstat (limited to 'ext/stringio')
-rw-r--r--ext/stringio/stringio.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 6c86e8964d..98b9d9aab1 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -1,3 +1,4 @@
+/* -*- mode: c; indent-tabs-mode: t -*- */
/**********************************************************************
stringio.c -
@@ -599,6 +600,14 @@ strio_closed_write(VALUE self)
return Qtrue;
}
+static struct StringIO *
+strio_to_read(VALUE self)
+{
+ struct StringIO *ptr = readable(self);
+ if (ptr->pos < RSTRING_LEN(ptr->string)) return ptr;
+ return NULL;
+}
+
/*
* call-seq:
* strio.eof -> true or false
@@ -610,8 +619,7 @@ strio_closed_write(VALUE self)
static VALUE
strio_eof(VALUE self)
{
- struct StringIO *ptr = readable(self);
- if (ptr->pos < RSTRING_LEN(ptr->string)) return Qfalse;
+ if (strio_to_read(self)) return Qfalse;
return Qtrue;
}
@@ -821,11 +829,11 @@ strio_get_sync(VALUE self)
static VALUE
strio_each_byte(VALUE self)
{
- struct StringIO *ptr = readable(self);
+ struct StringIO *ptr;
RETURN_ENUMERATOR(self, 0, 0);
- while (ptr->pos < RSTRING_LEN(ptr->string)) {
+ while ((ptr = strio_to_read(self)) != NULL) {
char c = RSTRING_PTR(ptr->string)[ptr->pos++];
rb_yield(CHR2FIX(c));
}
@@ -1064,11 +1072,7 @@ strio_each_codepoint(VALUE self)
ptr = readable(self);
enc = get_enc(ptr);
- for (;;) {
- if (ptr->pos >= RSTRING_LEN(ptr->string)) {
- return self;
- }
-
+ while ((ptr = strio_to_read(self)) != NULL) {
c = rb_enc_codepoint_len(RSTRING_PTR(ptr->string)+ptr->pos,
RSTRING_END(ptr->string), &n, enc);
ptr->pos += n;