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

ruby-changes:42524

From: nobu <ko1@a...>
Date: Fri, 15 Apr 2016 20:15:49 +0900 (JST)
Subject: [ruby-changes:42524] nobu:r54598 (trunk): thread.c: must be initialized to set name

nobu	2016-04-15 21:12:25 +0900 (Fri, 15 Apr 2016)

  New Revision: 54598

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54598

  Log:
    thread.c: must be initialized to set name
    
    * thread.c (get_initialized_threadptr): extract ensuring that the
      thread is initialized.
    * thread.c (rb_thread_setname): thread must be initialized to set
      the name.  [ruby-core:74963] [Bug #12290]

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_thread.rb
    trunk/thread.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54597)
+++ ChangeLog	(revision 54598)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Apr 15 21:12:23 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* thread.c (get_initialized_threadptr): extract ensuring that the
+	  thread is initialized.
+
+	* thread.c (rb_thread_setname): thread must be initialized to set
+	  the name.  [ruby-core:74963] [Bug #12290]
+
 Fri Apr 15 20:27:16 2016  SHIBATA Hiroshi  <hsbt@r...>
 
 	* lib/irb/ext/save-history.rb: Fix NoMethodError when method is not defined.
Index: thread.c
===================================================================
--- thread.c	(revision 54597)
+++ thread.c	(revision 54598)
@@ -711,6 +711,18 @@ thread_create_core(VALUE thval, VALUE ar https://github.com/ruby/ruby/blob/trunk/thread.c#L711
     return thval;
 }
 
+static rb_thread_t *
+get_initialized_threadptr(VALUE thread, VALUE klass)
+{
+    rb_thread_t *th;
+    GetThreadPtr(thread, th);
+    if (!th->first_args) {
+	rb_raise(rb_eThreadError, "uninitialized thread - check `%"PRIsVALUE"#initialize'",
+		 klass);
+    }
+    return th;
+}
+
 /*
  * call-seq:
  *  Thread.new { ... }			-> thread
@@ -734,18 +746,13 @@ thread_create_core(VALUE thval, VALUE ar https://github.com/ruby/ruby/blob/trunk/thread.c#L746
 static VALUE
 thread_s_new(int argc, VALUE *argv, VALUE klass)
 {
-    rb_thread_t *th;
     VALUE thread = rb_thread_alloc(klass);
 
     if (GET_VM()->main_thread->status == THREAD_KILLED)
 	rb_raise(rb_eThreadError, "can't alloc thread");
 
     rb_obj_call_init(thread, argc, argv);
-    GetThreadPtr(thread, th);
-    if (!th->first_args) {
-	rb_raise(rb_eThreadError, "uninitialized thread - check `%"PRIsVALUE"#initialize'",
-		 klass);
-    }
+    get_initialized_threadptr(thread, klass);
     return thread;
 }
 
@@ -2790,8 +2797,7 @@ rb_thread_setname(VALUE thread, VALUE na https://github.com/ruby/ruby/blob/trunk/thread.c#L2797
 #ifdef SET_ANOTHER_THREAD_NAME
     const char *s = "";
 #endif
-    rb_thread_t *th;
-    GetThreadPtr(thread, th);
+    rb_thread_t *th = get_initialized_threadptr(thread, RBASIC_CLASS(thread));
     if (!NIL_P(name)) {
 	rb_encoding *enc;
 	StringValueCStr(name);
@@ -2830,7 +2836,7 @@ rb_thread_inspect(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2836
     GetThreadPtr(thread, th);
     status = thread_status_name(th);
     str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread);
-    if (!NIL_P(th->name)) {
+    if (RTEST(th->name)) {
 	rb_str_catf(str, "@%"PRIsVALUE, th->name);
     }
     if (!th->first_func && th->first_proc) {
Index: test/ruby/test_thread.rb
===================================================================
--- test/ruby/test_thread.rb	(revision 54597)
+++ test/ruby/test_thread.rb	(revision 54598)
@@ -1098,4 +1098,10 @@ q.pop https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L1098
     t.kill
     t.join
   end
+
+  def test_thread_setname_uninitialized
+    bug12290 = '[ruby-core:74963] [Bug #12290]'
+    c = Class.new(Thread) {def initialize() self.name = "foo" end}
+    assert_raise(ThreadError, bug12290) {c.new {}}
+  end
 end

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

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