summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-28 08:49:25 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-28 08:49:25 +0000
commitdd8f7a6cfdce76078c1deb3d39e6f95edc1283c1 (patch)
tree1d6e283be15036c03e852f1f834bb7d3bda297aa
parent0a7785431c05d6b1854af94465f49c6b6d27a304 (diff)
* string.c: add internal API rb_str_locktmp_ensure().
* io.c (io_fread): use rb_str_locktmp_ensure(). [ruby-core:56121] [Bug #8669] * test/ruby/test_io.rb: add a test for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog9
-rw-r--r--internal.h1
-rw-r--r--io.c23
-rw-r--r--string.c7
-rw-r--r--test/ruby/test_io.rb14
5 files changed, 51 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 98690cc681f..610124417f1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Sun Jul 28 17:38:32 2013 Masaki Matsushita <glass.saga@gmail.com>
+
+ * string.c: add internal API rb_str_locktmp_ensure().
+
+ * io.c (io_fread): use rb_str_locktmp_ensure().
+ [ruby-core:56121] [Bug #8669]
+
+ * test/ruby/test_io.rb: add a test for above.
+
Sun Jul 28 13:04:39 2013 Masaki Matsushita <glass.saga@gmail.com>
* io.c (interpret_seek_whence): support SEEK_DATA and SEEK_HOLE.
diff --git a/internal.h b/internal.h
index b7a8a75669a..46d64b00fc7 100644
--- a/internal.h
+++ b/internal.h
@@ -419,6 +419,7 @@ VALUE rb_id_quote_unprintable(ID);
#define QUOTE(str) rb_str_quote_unprintable(str)
#define QUOTE_ID(id) rb_id_quote_unprintable(id)
void rb_str_fill_terminator(VALUE str, const int termlen);
+VALUE rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg);
/* struct.c */
VALUE rb_struct_init_copy(VALUE copy, VALUE s);
diff --git a/io.c b/io.c
index ad91df7a29c..850c5b1c468 100644
--- a/io.c
+++ b/io.c
@@ -2061,15 +2061,32 @@ io_bufread(char *ptr, long len, rb_io_t *fptr)
static void io_setstrbuf(VALUE *str, long len);
+struct bufread_arg {
+ char *str_ptr;
+ long len;
+ rb_io_t *fptr;
+};
+
+static VALUE
+bufread_call(VALUE arg)
+{
+ struct bufread_arg *p = (struct bufread_arg *)arg;
+ p->len = io_bufread(p->str_ptr, p->len, p->fptr);
+ return Qundef;
+}
+
static long
io_fread(VALUE str, long offset, long size, rb_io_t *fptr)
{
long len;
+ struct bufread_arg arg;
io_setstrbuf(&str, offset + size);
- rb_str_locktmp(str);
- len = io_bufread(RSTRING_PTR(str) + offset, size, fptr);
- rb_str_unlocktmp(str);
+ arg.str_ptr = RSTRING_PTR(str) + offset;
+ arg.len = size;
+ arg.fptr = fptr;
+ rb_str_locktmp_ensure(str, bufread_call, (VALUE)&arg);
+ len = arg.len;
if (len < 0) rb_sys_fail_path(fptr->pathv);
return len;
}
diff --git a/string.c b/string.c
index f101a94c320..e7f9b44324e 100644
--- a/string.c
+++ b/string.c
@@ -1871,6 +1871,13 @@ rb_str_unlocktmp(VALUE str)
return str;
}
+VALUE
+rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg)
+{
+ rb_str_locktmp(str);
+ return rb_ensure(func, arg, rb_str_unlocktmp, str);
+}
+
void
rb_str_set_len(VALUE str, long len)
{
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 469498cb0c0..55e034e3a13 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -2801,4 +2801,18 @@ End
end
}
end if /mswin|mingw/ =~ RUBY_PLATFORM
+
+ def test_read_unlocktmp_ensure
+ bug8669 = '[ruby-core:56121] [Bug #8669]'
+
+ str = ""
+ r, = IO.pipe
+ t = Thread.new { r.read(nil, str) }
+ sleep 0.1 until t.stop?
+ t.raise
+ sleep 0.1 while t.alive?
+ assert_nothing_raised(RuntimeError, bug8669) { str.clear }
+ ensure
+ t.kill
+ end
end