[前][次][番号順一覧][スレッド一覧]

ruby-changes:30160

From: glass <ko1@a...>
Date: Sun, 28 Jul 2013 17:49:39 +0900 (JST)
Subject: [ruby-changes:30160] glass:r42212 (trunk): * string.c: add internal API rb_str_locktmp_ensure().

glass	2013-07-28 17:49:25 +0900 (Sun, 28 Jul 2013)

  New Revision: 42212

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42212

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/internal.h
    trunk/io.c
    trunk/string.c
    trunk/test/ruby/test_io.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42211)
+++ ChangeLog	(revision 42212)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jul 28 17:38:32 2013  Masaki Matsushita  <glass.saga@g...>
+
+	* 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@g...>
 
 	* io.c (interpret_seek_whence): support SEEK_DATA and SEEK_HOLE.
Index: string.c
===================================================================
--- string.c	(revision 42211)
+++ string.c	(revision 42212)
@@ -1871,6 +1871,13 @@ rb_str_unlocktmp(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L1871
     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)
 {
Index: io.c
===================================================================
--- io.c	(revision 42211)
+++ io.c	(revision 42212)
@@ -2061,15 +2061,32 @@ io_bufread(char *ptr, long len, rb_io_t https://github.com/ruby/ruby/blob/trunk/io.c#L2061
 
 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;
 }
Index: internal.h
===================================================================
--- internal.h	(revision 42211)
+++ internal.h	(revision 42212)
@@ -419,6 +419,7 @@ VALUE rb_id_quote_unprintable(ID); https://github.com/ruby/ruby/blob/trunk/internal.h#L419
 #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);
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 42211)
+++ test/ruby/test_io.rb	(revision 42212)
@@ -2801,4 +2801,18 @@ End https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L2801
       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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]