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

ruby-changes:28048

From: nobu <ko1@a...>
Date: Thu, 4 Apr 2013 16:54:55 +0900 (JST)
Subject: [ruby-changes:28048] nobu:r40100 (trunk): object.c: avoid inadvertent symbol creation

nobu	2013-04-04 16:54:44 +0900 (Thu, 04 Apr 2013)

  New Revision: 40100

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

  Log:
    object.c: avoid inadvertent symbol creation
    
    * object.c (rb_mod_const_set): avoid inadvertent symbol creation.
      (rb_obj_ivar_set): ditto.
      (rb_mod_cvar_set): ditto.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40099)
+++ ChangeLog	(revision 40100)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Apr  4 16:54:40 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* object.c (rb_mod_const_set): avoid inadvertent symbol creation.
+	  (rb_obj_ivar_set): ditto.
+	  (rb_mod_cvar_set): ditto.
+
 Thu Apr  4 15:46:48 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* enum.c (enum_inject): avoid inadvertent symbol creation.
Index: object.c
===================================================================
--- object.c	(revision 40099)
+++ object.c	(revision 40100)
@@ -2043,13 +2043,11 @@ rb_mod_const_get(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/object.c#L2043
 static VALUE
 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
 {
-    ID id = rb_to_id(name);
-
-    if (!rb_is_const_id(id)) {
-	rb_name_error(id, "wrong constant name %"PRIsVALUE,
-		      QUOTE_ID(id));
+    if (!SYMBOL_P(name) && !rb_is_const_name(name)) {
+	rb_name_error_str(name, "wrong constant name %"PRIsVALUE,
+			  QUOTE(name));
     }
-    rb_const_set(mod, id, value);
+    rb_const_set(mod, rb_to_id(name), value);
     return value;
 }
 
@@ -2166,13 +2164,11 @@ rb_obj_ivar_get(VALUE obj, VALUE iv) https://github.com/ruby/ruby/blob/trunk/object.c#L2164
 static VALUE
 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
 {
-    ID id = rb_to_id(iv);
-
-    if (!rb_is_instance_id(id)) {
-	rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
-		      QUOTE_ID(id));
+    if (!SYMBOL_P(iv) && !rb_is_instance_name(iv)) {
+	rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
+			  QUOTE(iv));
     }
-    return rb_ivar_set(obj, id, val);
+    return rb_ivar_set(obj, rb_to_id(iv), val);
 }
 
 /*
@@ -2277,13 +2273,11 @@ rb_mod_cvar_get(VALUE obj, VALUE iv) https://github.com/ruby/ruby/blob/trunk/object.c#L2273
 static VALUE
 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
 {
-    ID id = rb_to_id(iv);
-
-    if (!rb_is_class_id(id)) {
-	rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
-		      QUOTE_ID(id));
+    if (!SYMBOL_P(iv) && !rb_is_class_id(iv)) {
+	rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
+			  QUOTE(iv));
     }
-    rb_cvar_set(obj, id, val);
+    rb_cvar_set(obj, rb_to_id(iv), val);
     return val;
 }
 
Index: test/-ext-/symbol/test_inadvertent_creation.rb
===================================================================
--- test/-ext-/symbol/test_inadvertent_creation.rb	(revision 40099)
+++ test/-ext-/symbol/test_inadvertent_creation.rb	(revision 40100)
@@ -193,5 +193,26 @@ module Test_Symbol https://github.com/ruby/ruby/blob/trunk/test/-ext-/symbol/test_inadvertent_creation.rb#L193
       assert_raise(NoMethodError) {[1, 2].inject(name)}
       assert_not_send([Bug::Symbol, :interned?, name])
     end
+
+    def test_module_const_set
+      name = noninterned_name
+      mod = Module.new
+      assert_raise(NameError) {mod.const_set(name, true)}
+      assert_not_send([Bug::Symbol, :interned?, name])
+    end
+
+    def test_module_cvar_set
+      name = noninterned_name
+      mod = Module.new
+      assert_raise(NameError) {mod.class_variable_set(name, true)}
+      assert_not_send([Bug::Symbol, :interned?, name])
+    end
+
+    def test_object_ivar_set
+      name = noninterned_name
+      obj = Object.new
+      assert_raise(NameError) {obj.instance_variable_set(name, true)}
+      assert_not_send([Bug::Symbol, :interned?, name])
+    end
   end
 end

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

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