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

ruby-changes:32118

From: nobu <ko1@a...>
Date: Sat, 14 Dec 2013 12:38:28 +0900 (JST)
Subject: [ruby-changes:32118] nobu:r44197 (trunk): object.c: check const names

nobu	2013-12-14 12:38:22 +0900 (Sat, 14 Dec 2013)

  New Revision: 44197

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

  Log:
    object.c: check const names
    
    * object.c (rb_mod_const_get, rb_mod_const_defined): check
      constant names more strictly.

  Modified files:
    trunk/object.c
    trunk/test/ruby/test_module.rb
Index: object.c
===================================================================
--- object.c	(revision 44196)
+++ object.c	(revision 44197)
@@ -2134,16 +2134,16 @@ rb_mod_const_get(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/object.c#L2134
 	}
 
 	if (!id) {
-	    if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) {
-		part = rb_str_subseq(name, beglen, len);
+	    part = rb_str_subseq(name, beglen, len);
+	    OBJ_FREEZE(part);
+	    if (!ISUPPER(*pbeg) || !rb_is_const_name(part)) {
 		rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
 				  QUOTE(part));
 	    }
 	    else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
-		id = rb_intern3(pbeg, len, enc);
+		id = rb_intern_str(part);
 	    }
 	    else {
-		part = rb_str_subseq(name, beglen, len);
 		rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"%"PRIsVALUE,
 				  rb_str_subseq(name, 0, beglen),
 				  QUOTE(part));
@@ -2271,8 +2271,9 @@ rb_mod_const_defined(int argc, VALUE *ar https://github.com/ruby/ruby/blob/trunk/object.c#L2271
 	}
 
 	if (!id) {
-	    if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) {
-		part = rb_str_subseq(name, beglen, len);
+	    part = rb_str_subseq(name, beglen, len);
+	    OBJ_FREEZE(part);
+	    if (!ISUPPER(*pbeg) || !rb_is_const_name(part)) {
 		rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
 				  QUOTE(part));
 	    }
Index: test/ruby/test_module.rb
===================================================================
--- test/ruby/test_module.rb	(revision 44196)
+++ test/ruby/test_module.rb	(revision 44197)
@@ -240,7 +240,7 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L240
     assert_not_operator(Math, :const_defined?, "IP")
   end
 
-  def test_bad_constants
+  def each_bad_constants(m, &b)
     [
       "#<Class:0x7b8b718b>",
       ":Object",
@@ -248,15 +248,28 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L248
       ":",
       ["String::", "[Bug #7573]"],
       "\u3042",
+      "Name?",
     ].each do |name, msg|
       expected = "wrong constant name %s" % quote(name)
       msg = "#{msg}#{': ' if msg}wrong constant name #{name.dump}"
-      assert_raise_with_message(NameError, expected, msg) {
-        Object.const_get name
-      }
+      assert_raise_with_message(NameError, expected, "#{msg} to #{m}") do
+        yield name
+      end
     end
   end
 
+  def test_bad_constants_get
+    each_bad_constants("get") {|name|
+      Object.const_get name
+    }
+  end
+
+  def test_bad_constants_defined
+    each_bad_constants("defined?") {|name|
+      Object.const_defined? name
+    }
+  end
+
   def test_leading_colons
     assert_equal Object, AClass.const_get('::Object')
   end

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

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