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

ruby-changes:28044

From: nobu <ko1@a...>
Date: Thu, 4 Apr 2013 14:37:31 +0900 (JST)
Subject: [ruby-changes:28044] nobu:r40096 (trunk): thread.c: avoid inadvertent symbol creation

nobu	2013-04-04 14:37:21 +0900 (Thu, 04 Apr 2013)

  New Revision: 40096

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

  Log:
    thread.c: avoid inadvertent symbol creation
    
    * thread.c (rb_thread_aref): avoid inadvertent symbol creation.
      (rb_thread_variable_get): ditto.
      (rb_thread_key_p): ditto.
      (rb_thread_variable_p): ditto.

  Modified files:
    trunk/ChangeLog
    trunk/test/-ext-/symbol/test_inadvertent_creation.rb
    trunk/thread.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40095)
+++ ChangeLog	(revision 40096)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Apr  4 14:37:07 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* thread.c (rb_thread_aref): avoid inadvertent symbol creation.
+	  (rb_thread_variable_get): ditto.
+	  (rb_thread_key_p): ditto.
+	  (rb_thread_variable_p): ditto.
+
 Thu Apr  4 11:33:57 2013  NARUSE, Yui  <naruse@r...>
 
 	* ext/openssl/ossl_bn.c (ossl_bn_to_i): Use bn2hex to speed up.
Index: thread.c
===================================================================
--- thread.c	(revision 40095)
+++ thread.c	(revision 40096)
@@ -2784,9 +2784,11 @@ rb_thread_local_aref(VALUE thread, ID id https://github.com/ruby/ruby/blob/trunk/thread.c#L2784
  */
 
 static VALUE
-rb_thread_aref(VALUE thread, VALUE id)
+rb_thread_aref(VALUE thread, VALUE key)
 {
-    return rb_thread_local_aref(thread, rb_to_id(id));
+    ID id = rb_check_id(&key);
+    if (!id) return Qnil;
+    return rb_thread_local_aref(thread, id);
 }
 
 VALUE
@@ -2862,10 +2864,11 @@ rb_thread_aset(VALUE self, VALUE id, VAL https://github.com/ruby/ruby/blob/trunk/thread.c#L2864
  */
 
 static VALUE
-rb_thread_variable_get(VALUE thread, VALUE id)
+rb_thread_variable_get(VALUE thread, VALUE key)
 {
     VALUE locals;
     rb_thread_t *th;
+    ID id = rb_check_id(&key);
 
     GetThreadPtr(thread, th);
 
@@ -2873,8 +2876,9 @@ rb_thread_variable_get(VALUE thread, VAL https://github.com/ruby/ruby/blob/trunk/thread.c#L2876
 	rb_raise(rb_eSecurityError, "Insecure: can't access thread locals");
     }
 
+    if (!id) return Qnil;
     locals = rb_iv_get(thread, "locals");
-    return rb_hash_aref(locals, ID2SYM(rb_to_id(id)));
+    return rb_hash_aref(locals, ID2SYM(id));
 }
 
 /*
@@ -2922,11 +2926,11 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/thread.c#L2926
 rb_thread_key_p(VALUE self, VALUE key)
 {
     rb_thread_t *th;
-    ID id = rb_to_id(key);
+    ID id = rb_check_id(&key);
 
     GetThreadPtr(self, th);
 
-    if (!th->local_storage) {
+    if (!id || !th->local_storage) {
 	return Qfalse;
     }
     if (st_lookup(th->local_storage, id, 0)) {
@@ -3043,13 +3047,16 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/thread.c#L3047
 rb_thread_variable_p(VALUE thread, VALUE key)
 {
     VALUE locals;
+    ID id = rb_check_id(&key);
+
+    if (!id) return Qfalse;
 
     locals = rb_iv_get(thread, "locals");
 
     if (!RHASH(locals)->ntbl)
         return Qfalse;
 
-    if (st_lookup(RHASH(locals)->ntbl, ID2SYM(rb_to_id(key)), 0)) {
+    if (st_lookup(RHASH(locals)->ntbl, ID2SYM(id), 0)) {
 	return Qtrue;
     }
 
Index: test/-ext-/symbol/test_inadvertent_creation.rb
===================================================================
--- test/-ext-/symbol/test_inadvertent_creation.rb	(revision 40095)
+++ test/-ext-/symbol/test_inadvertent_creation.rb	(revision 40096)
@@ -159,5 +159,33 @@ module Test_Symbol https://github.com/ruby/ruby/blob/trunk/test/-ext-/symbol/test_inadvertent_creation.rb#L159
       assert_equal(name, e.name)
       assert_equal([Feature5112], e.args)
     end
+
+    def test_thread_aref
+      Thread.current[:test] = nil
+      name = noninterned_name
+      assert_nil(Thread.current[name])
+      assert_not_send([Bug::Symbol, :interned?, name])
+    end
+
+    def test_thread_key?
+      Thread.current[:test] = nil
+      name = noninterned_name
+      assert_not_send([Thread.current, :key?, name])
+      assert_not_send([Bug::Symbol, :interned?, name])
+    end
+
+    def test_thread_variable_get
+      Thread.current.thread_variable_set(:test, nil)
+      name = noninterned_name
+      assert_nil(Thread.current.thread_variable_get(name))
+      assert_not_send([Bug::Symbol, :interned?, name])
+    end
+
+    def test_thread_variable?
+      Thread.current.thread_variable_set(:test, nil)
+      name = noninterned_name
+      assert_not_send([Thread.current, :thread_variable?, name])
+      assert_not_send([Bug::Symbol, :interned?, name])
+    end
   end
 end

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

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