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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-04-15 12:12:25 +00:00
parent 589169cb78
commit f7d0059e36
3 changed files with 29 additions and 9 deletions

View File

@ -1,3 +1,11 @@
Fri Apr 15 21:12:23 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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@ruby-lang.org> Fri Apr 15 20:27:16 2016 SHIBATA Hiroshi <hsbt@ruby-lang.org>
* lib/irb/ext/save-history.rb: Fix NoMethodError when method is not defined. * lib/irb/ext/save-history.rb: Fix NoMethodError when method is not defined.

View File

@ -1098,4 +1098,10 @@ q.pop
t.kill t.kill
t.join t.join
end 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 end

View File

@ -711,6 +711,18 @@ thread_create_core(VALUE thval, VALUE args, VALUE (*fn)(ANYARGS))
return thval; 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: * call-seq:
* Thread.new { ... } -> thread * Thread.new { ... } -> thread
@ -734,18 +746,13 @@ thread_create_core(VALUE thval, VALUE args, VALUE (*fn)(ANYARGS))
static VALUE static VALUE
thread_s_new(int argc, VALUE *argv, VALUE klass) thread_s_new(int argc, VALUE *argv, VALUE klass)
{ {
rb_thread_t *th;
VALUE thread = rb_thread_alloc(klass); VALUE thread = rb_thread_alloc(klass);
if (GET_VM()->main_thread->status == THREAD_KILLED) if (GET_VM()->main_thread->status == THREAD_KILLED)
rb_raise(rb_eThreadError, "can't alloc thread"); rb_raise(rb_eThreadError, "can't alloc thread");
rb_obj_call_init(thread, argc, argv); rb_obj_call_init(thread, argc, argv);
GetThreadPtr(thread, th); get_initialized_threadptr(thread, klass);
if (!th->first_args) {
rb_raise(rb_eThreadError, "uninitialized thread - check `%"PRIsVALUE"#initialize'",
klass);
}
return thread; return thread;
} }
@ -2790,8 +2797,7 @@ rb_thread_setname(VALUE thread, VALUE name)
#ifdef SET_ANOTHER_THREAD_NAME #ifdef SET_ANOTHER_THREAD_NAME
const char *s = ""; const char *s = "";
#endif #endif
rb_thread_t *th; rb_thread_t *th = get_initialized_threadptr(thread, RBASIC_CLASS(thread));
GetThreadPtr(thread, th);
if (!NIL_P(name)) { if (!NIL_P(name)) {
rb_encoding *enc; rb_encoding *enc;
StringValueCStr(name); StringValueCStr(name);
@ -2830,7 +2836,7 @@ rb_thread_inspect(VALUE thread)
GetThreadPtr(thread, th); GetThreadPtr(thread, th);
status = thread_status_name(th); status = thread_status_name(th);
str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread); str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread);
if (!NIL_P(th->name)) { if (RTEST(th->name)) {
rb_str_catf(str, "@%"PRIsVALUE, th->name); rb_str_catf(str, "@%"PRIsVALUE, th->name);
} }
if (!th->first_func && th->first_proc) { if (!th->first_func && th->first_proc) {