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

ruby-changes:32766

From: nagachika <ko1@a...>
Date: Wed, 5 Feb 2014 23:51:49 +0900 (JST)
Subject: [ruby-changes:32766] nagachika:r44845 (ruby_2_0_0): merge revision(s) r42988: [Backport #8928]

nagachika	2014-02-05 23:51:42 +0900 (Wed, 05 Feb 2014)

  New Revision: 44845

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

  Log:
    merge revision(s) r42988: [Backport #8928]
    
    * parse.y (intern_str): sigil only names are junk, at least one
      identifier character is needed.  [ruby-dev:47723] [Bug #8928]
    
    * parse.y (rb_enc_symname_type): fix out of bound access.

  Added files:
    branches/ruby_2_0_0/ext/-test-/symbol/type.c
    branches/ruby_2_0_0/test/-ext-/symbol/test_type.rb
  Modified directories:
    branches/ruby_2_0_0/
  Modified files:
    branches/ruby_2_0_0/ChangeLog
    branches/ruby_2_0_0/parse.y
    branches/ruby_2_0_0/test/ruby/test_module.rb
    branches/ruby_2_0_0/test/ruby/test_object.rb
    branches/ruby_2_0_0/version.h
Index: ruby_2_0_0/ChangeLog
===================================================================
--- ruby_2_0_0/ChangeLog	(revision 44844)
+++ ruby_2_0_0/ChangeLog	(revision 44845)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ChangeLog#L1
+Wed Feb  5 23:39:36 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (intern_str): sigil only names are junk, at least one
+	  identifier character is needed.  [ruby-dev:47723] [Bug #8928]
+
+	* parse.y (rb_enc_symname_type): fix out of bound access.
+
 Wed Feb  5 22:54:52 2014  Kazuki Tsujimoto  <kazuki@c...>
 
 	* time.c (get_timeval, get_new_timeval): use rb_obj_class()
Index: ruby_2_0_0/parse.y
===================================================================
--- ruby_2_0_0/parse.y	(revision 44844)
+++ ruby_2_0_0/parse.y	(revision 44845)
@@ -10136,6 +10136,7 @@ rb_enc_symname_type(const char *name, lo https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/parse.y#L10136
 	if (m >= e || (*m != '_' && !rb_enc_isalpha(*m, enc) && ISASCII(*m)))
 	    return -1;
 	while (m < e && is_identchar(m, e, enc)) m += rb_enc_mbclen(m, e, enc);
+	if (m >= e) break;
 	switch (*m) {
 	  case '!': case '?':
 	    if (type == ID_GLOBAL || type == ID_CLASS || type == ID_INSTANCE) return -1;
@@ -10243,7 +10244,8 @@ intern_str(VALUE str) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/parse.y#L10244
     enc = rb_enc_get(str);
     symenc = enc;
 
-    if (rb_cString && !rb_enc_asciicompat(enc)) {
+    if (!len || (rb_cString && !rb_enc_asciicompat(enc))) {
+      junk:
 	id = ID_JUNK;
 	goto new_id;
     }
@@ -10251,6 +10253,7 @@ intern_str(VALUE str) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/parse.y#L10253
     id = 0;
     switch (*m) {
       case '$':
+	if (len < 2) goto junk;
 	id |= ID_GLOBAL;
 	if ((mb = is_special_global_name(++m, e, enc)) != 0) {
 	    if (!--mb) symenc = rb_usascii_encoding();
@@ -10259,10 +10262,12 @@ intern_str(VALUE str) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/parse.y#L10262
 	break;
       case '@':
 	if (m[1] == '@') {
+	    if (len < 3) goto junk;
 	    m++;
 	    id |= ID_CLASS;
 	}
 	else {
+	    if (len < 2) goto junk;
 	    id |= ID_INSTANCE;
 	}
 	m++;
@@ -10288,6 +10293,8 @@ intern_str(VALUE str) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/parse.y#L10293
 
 	if (m[last] == '=') {
 	    /* attribute assignment */
+	    if (!rb_enc_symname2_p(name, last, enc))
+		goto junk;
 	    id = rb_intern3(name, last, enc);
 	    if (id > tLAST_OP_ID && !is_attrset_id(id)) {
 		enc = rb_enc_get(rb_id2str(id));
Index: ruby_2_0_0/ext/-test-/symbol/type.c
===================================================================
--- ruby_2_0_0/ext/-test-/symbol/type.c	(revision 0)
+++ ruby_2_0_0/ext/-test-/symbol/type.c	(revision 44845)
@@ -0,0 +1,34 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ext/-test-/symbol/type.c#L1
+#include "ruby.h"
+
+#ifdef HAVE_RB_IS_CONST_NAME
+# define get_symbol_type(type, t, name) do { \
+	ID id = rb_check_id(&name); \
+	t = (id ? rb_is_##type##_id(id) : rb_is_##type##_name(name)); \
+    } while (0)
+#else
+# define get_symbol_type(type, t, name) do { \
+	t = rb_is_##type##_id(rb_to_id(name)); \
+    } while (0)
+#endif
+
+#define define_symbol_type_p(type) \
+static VALUE \
+bug_sym_##type##_p(VALUE self, VALUE name) \
+{ \
+    int t; \
+    get_symbol_type(type, t, name); \
+    return (t ? Qtrue : Qfalse); \
+}
+
+#define declare_symbol_type_p(type) \
+    rb_define_singleton_method(klass, #type"?", bug_sym_##type##_p, 1);
+
+#define FOREACH_ID_TYPES(x) x(const) x(class) x(global) x(instance) x(attrset) x(local) x(junk)
+
+FOREACH_ID_TYPES(define_symbol_type_p)
+
+void
+Init_type(VALUE klass)
+{
+    FOREACH_ID_TYPES(declare_symbol_type_p)
+}

Property changes on: ruby_2_0_0/ext/-test-/symbol/type.c
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: ruby_2_0_0/version.h
===================================================================
--- ruby_2_0_0/version.h	(revision 44844)
+++ ruby_2_0_0/version.h	(revision 44845)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/version.h#L1
 #define RUBY_VERSION "2.0.0"
 #define RUBY_RELEASE_DATE "2014-02-05"
-#define RUBY_PATCHLEVEL 395
+#define RUBY_PATCHLEVEL 396
 
 #define RUBY_RELEASE_YEAR 2014
 #define RUBY_RELEASE_MONTH 2
Index: ruby_2_0_0/test/ruby/test_module.rb
===================================================================
--- ruby_2_0_0/test/ruby/test_module.rb	(revision 44844)
+++ ruby_2_0_0/test/ruby/test_module.rb	(revision 44845)
@@ -663,6 +663,8 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_module.rb#L663
     c.class_eval('@@foo = :foo')
     assert_equal(:foo, c.class_variable_get(:@@foo))
     assert_raise(NameError) { c.class_variable_get(:@@bar) } # c.f. instance_variable_get
+    assert_raise(NameError) { c.class_variable_get(:'@@') }
+    assert_raise(NameError) { c.class_variable_get('@@') }
     assert_raise(NameError) { c.class_variable_get(:foo) }
   end
 
@@ -670,6 +672,8 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_module.rb#L672
     c = Class.new
     c.class_variable_set(:@@foo, :foo)
     assert_equal(:foo, c.class_eval('@@foo'))
+    assert_raise(NameError) { c.class_variable_set(:'@@', 1) }
+    assert_raise(NameError) { c.class_variable_set('@@', 1) }
     assert_raise(NameError) { c.class_variable_set(:foo, 1) }
   end
 
@@ -678,6 +682,8 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_module.rb#L682
     c.class_eval('@@foo = :foo')
     assert_equal(true, c.class_variable_defined?(:@@foo))
     assert_equal(false, c.class_variable_defined?(:@@bar))
+    assert_raise(NameError) { c.class_variable_defined?(:'@@') }
+    assert_raise(NameError) { c.class_variable_defined?('@@') }
     assert_raise(NameError) { c.class_variable_defined?(:foo) }
   end
 
Index: ruby_2_0_0/test/ruby/test_object.rb
===================================================================
--- ruby_2_0_0/test/ruby/test_object.rb	(revision 44844)
+++ ruby_2_0_0/test/ruby/test_object.rb	(revision 44845)
@@ -164,6 +164,8 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_object.rb#L164
     o.instance_eval { @foo = :foo }
     assert_equal(:foo, o.instance_variable_get(:@foo))
     assert_equal(nil, o.instance_variable_get(:@bar))
+    assert_raise(NameError) { o.instance_variable_get('@') }
+    assert_raise(NameError) { o.instance_variable_get(:'@') }
     assert_raise(NameError) { o.instance_variable_get(:foo) }
   end
 
@@ -171,6 +173,8 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_object.rb#L173
     o = Object.new
     o.instance_variable_set(:@foo, :foo)
     assert_equal(:foo, o.instance_eval { @foo })
+    assert_raise(NameError) { o.instance_variable_set(:'@', 1) }
+    assert_raise(NameError) { o.instance_variable_set('@', 1) }
     assert_raise(NameError) { o.instance_variable_set(:foo, 1) }
   end
 
@@ -179,6 +183,8 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_object.rb#L183
     o.instance_eval { @foo = :foo }
     assert_equal(true, o.instance_variable_defined?(:@foo))
     assert_equal(false, o.instance_variable_defined?(:@bar))
+    assert_raise(NameError) { o.instance_variable_defined?(:'@') }
+    assert_raise(NameError) { o.instance_variable_defined?('@') }
     assert_raise(NameError) { o.instance_variable_defined?(:foo) }
   end
 
Index: ruby_2_0_0/test/-ext-/symbol/test_type.rb
===================================================================
--- ruby_2_0_0/test/-ext-/symbol/test_type.rb	(revision 0)
+++ ruby_2_0_0/test/-ext-/symbol/test_type.rb	(revision 44845)
@@ -0,0 +1,96 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/-ext-/symbol/test_type.rb#L1
+require 'test/unit'
+require "-test-/symbol"
+
+module Test_Symbol
+  class TestType < Test::Unit::TestCase
+    def assert_symtype(sym, pred, msg = nil)
+      assert_send([Bug::Symbol, pred, sym], msg)
+    end
+
+    def assert_not_symtype(sym, pred, msg = nil)
+      assert_not_send([Bug::Symbol, pred, sym], msg)
+    end
+
+    def test_const
+      assert_symtype("Foo", :const?)
+      assert_not_symtype("F!", :const?)
+      assert_not_symtype("foo", :const?)
+      assert_not_symtype("@foo", :const?)
+      assert_not_symtype("@@foo", :const?)
+      assert_not_symtype("$foo", :const?)
+      assert_not_symtype("foo=", :const?)
+      assert_not_symtype("[foo]", :const?)
+      assert_not_symtype("xFoo", :const?)
+    end
+
+    def test_local
+      assert_symtype("foo", :local?)
+      assert_symtype("fooBar", :local?)
+      assert_symtype("foo_bar", :local?)
+      assert_not_symtype("foo!", :local?)
+      assert_not_symtype("foo?", :local?)
+      assert_not_symtype("Foo", :local?)
+      assert_not_symtype("@foo", :local?)
+      assert_not_symtype("@@foo", :local?)
+      assert_not_symtype("$foo", :local?)
+      assert_not_symtype("foo=", :local?)
+      assert_not_symtype("[foo]", :local?)
+    end
+
+    def test_global
+      assert_symtype("$foo", :global?)
+      assert_symtype("$$", :global?)
+      assert_not_symtype("$()", :global?)
+      assert_not_symtype("$", :global?)
+      assert_not_symtype("foo", :global?)
+      assert_not_symtype("Foo", :global?)
+      assert_not_symtype("@foo", :global?)
+      assert_not_symtype("@@foo", :global?)
+      assert_not_symtype("foo=", :global?)
+      assert_not_symtype("[foo]", :global?)
+    end
+
+    def test_instance
+      assert_symtype("@foo", :instance?)
+      assert_not_symtype("@", :instance?)
+      assert_not_symtype("@1", :instance?)
+      assert_not_symtype("@@", :instance?)
+      assert_not_symtype("foo", :instance?)
+      assert_not_symtype("Foo", :instance?)
+      assert_not_symtype("@@foo", :instance?)
+      assert_not_symtype("$foo", :instance?)
+      assert_not_symtype("foo=", :instance?)
+      assert_not_symtype("[foo]", :instance?)
+    end
+
+    def test_class
+      assert_symtype("@@foo", :class?)
+      assert_not_symtype("@@", :class?)
+      assert_not_symtype("@", :class?)
+      assert_not_symtype("@@1", :class?)
+      assert_not_symtype("foo", :class?)
+      assert_not_symtype("Foo", :class?)
+      assert_not_symtype("@foo", :class?)
+      assert_not_symtype("$foo", :class?)
+      assert_not_symtype("foo=", :class?)
+      assert_not_symtype("[foo]", :class?)
+    end
+
+    def test_attrset
+      assert_symtype("foo=", :attrset?)
+      assert_symtype("Foo=", :attrset?)
+      assert_symtype("@foo=", :attrset?)
+      assert_symtype("@@foo=", :attrset?)
+      assert_symtype("$foo=", :attrset?)
+      assert_not_symtype("0=", :attrset?)
+      assert_not_symtype("@=", :attrset?)
+      assert_not_symtype("@@=", :attrset?)
+      assert_not_symtype("foo", :attrset?)
+      assert_not_symtype("Foo", :attrset?)
+      assert_not_symtype("@foo", :attrset?)
+      assert_not_symtype("@@foo", :attrset?)
+      assert_not_symtype("$foo", :attrset?)
+      assert_not_symtype("[foo]", :attrset?)
+    end
+  end
+end

Property changes on: ruby_2_0_0/test/-ext-/symbol/test_type.rb
___________________________________________________________________
Added: svn:eol-style
   + LF


Property changes on: ruby_2_0_0
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r42988


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

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