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

ruby-changes:30383

From: usa <ko1@a...>
Date: Fri, 9 Aug 2013 16:22:59 +0900 (JST)
Subject: [ruby-changes:30383] usa:r42462 (ruby_1_9_3): merge revision(s) 40606, 40607, 40635: [Backport #8375]

usa	2013-08-09 16:22:51 +0900 (Fri, 09 Aug 2013)

  New Revision: 42462

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

  Log:
    merge revision(s) 40606,40607,40635: [Backport #8375]
    
    test_scanner_events.rb: assert_location
    
    * test/ripper/test_scanner_events.rb (TestRipper#assert_location):
      rename so skipped in backtraces.
    * parse.y (parser_yylex): fail if $, @, @@ are not followed by a valid
      name character.  [ruby-core:54846] [Bug #8375].
    
    * parse.y (parser_peek_variable_name): treat invalid global, class,
      and instance variable names as mere strings rather than errors.
      [ruby-core:54885] [Bug #8375]

  Modified directories:
    branches/ruby_1_9_3/
  Modified files:
    branches/ruby_1_9_3/ChangeLog
    branches/ruby_1_9_3/parse.y
    branches/ruby_1_9_3/test/ripper/test_parser_events.rb
    branches/ruby_1_9_3/test/ripper/test_scanner_events.rb
    branches/ruby_1_9_3/test/ruby/test_parse.rb
    branches/ruby_1_9_3/version.h
Index: ruby_1_9_3/ChangeLog
===================================================================
--- ruby_1_9_3/ChangeLog	(revision 42461)
+++ ruby_1_9_3/ChangeLog	(revision 42462)
@@ -1,3 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ChangeLog#L1
+Fri Aug  9 15:59:22 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (parser_peek_variable_name): treat invalid global, class,
+	  and instance variable names as mere strings rather than errors.
+	  [ruby-core:54885] [Bug #8375]
+
+Fri Aug  9 15:59:22 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (parser_yylex): fail if $, @, @@ are not followed by a valid
+	  name character.  [ruby-core:54846] [Bug #8375].
+
 Fri Aug  9 15:56:05 2013  NAKAMURA Usaku  <usa@r...>
 
 	* lib/net/http.rb (Net::HTTP#send_request_with_body_stream): use
Index: ruby_1_9_3/parse.y
===================================================================
--- ruby_1_9_3/parse.y	(revision 42461)
+++ ruby_1_9_3/parse.y	(revision 42462)
@@ -6004,6 +6004,70 @@ ripper_flush_string_content(struct parse https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/parse.y#L6004
 #define flush_string_content(enc) ((void)(enc))
 #endif
 
+RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
+/* this can be shared with ripper, since it's independent from struct
+ * parser_params. */
+#ifndef RIPPER
+#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
+#define SPECIAL_PUNCT(idx) ( \
+	BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
+	BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
+	BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
+	BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
+	BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
+	BIT('0', idx))
+const unsigned int ruby_global_name_punct_bits[] = {
+    SPECIAL_PUNCT(0),
+    SPECIAL_PUNCT(1),
+    SPECIAL_PUNCT(2),
+};
+#undef BIT
+#undef SPECIAL_PUNCT
+#endif
+
+static inline int
+is_global_name_punct(const char c)
+{
+    if (c <= 0x20 || 0x7e < c) return 0;
+    return (ruby_global_name_punct_bits[(c - 0x20) / 32] >> (c % 32)) & 1;
+}
+
+static int
+parser_peek_variable_name(struct parser_params *parser)
+{
+    int c;
+    const char *p = lex_p;
+
+    if (p + 1 >= lex_pend) return 0;
+    c = *p++;
+    switch (c) {
+      case '$':
+	if ((c = *p) == '-') {
+	    if (++p >= lex_pend) return 0;
+	    c = *p;
+	}
+	else if (is_global_name_punct(c) || ISDIGIT(c)) {
+	    return tSTRING_DVAR;
+	}
+	break;
+      case '@':
+	if ((c = *p) == '@') {
+	    if (++p >= lex_pend) return 0;
+	    c = *p;
+	}
+	break;
+      case '{':
+	lex_p = p;
+	command_start = TRUE;
+	return tSTRING_DBEG;
+      default:
+	return 0;
+    }
+    if (!ISASCII(c) || c == '_' || ISALPHA(c))
+	return tSTRING_DVAR;
+    return 0;
+}
+
 static int
 parser_parse_string(struct parser_params *parser, NODE *quote)
 {
@@ -6034,15 +6098,10 @@ parser_parse_string(struct parser_params https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/parse.y#L6098
     }
     newtok();
     if ((func & STR_FUNC_EXPAND) && c == '#') {
-	switch (c = nextc()) {
-	  case '$':
-	  case '@':
-	    pushback(c);
-	    return tSTRING_DVAR;
-	  case '{':
-	    return tSTRING_DBEG;
-	}
+	int t = parser_peek_variable_name(parser);
+	if (t) return t;
 	tokadd('#');
+	c = nextc();
     }
     pushback(c);
     if (tokadd_string(func, term, paren, &quote->nd_nest,
@@ -6249,15 +6308,10 @@ parser_here_document(struct parser_param https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/parse.y#L6308
 	/*	int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
 	newtok();
 	if (c == '#') {
-	    switch (c = nextc()) {
-	      case '$':
-	      case '@':
-		pushback(c);
-		return tSTRING_DVAR;
-	      case '{':
-		return tSTRING_DBEG;
-	    }
+	    int t = parser_peek_variable_name(parser);
+	    if (t) return t;
 	    tokadd('#');
+	    c = nextc();
 	}
 	do {
 	    pushback(c);
@@ -7743,7 +7797,8 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/parse.y#L7797
 	  default:
 	    if (!parser_is_identchar()) {
 		pushback(c);
-		return '$';
+		compile_error(PARSER_ARG "`$%c' is not allowed as a global variable name", c);
+		return 0;
 	    }
 	  case '0':
 	    tokadd('$');
@@ -7758,7 +7813,8 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/parse.y#L7813
 	    tokadd('@');
 	    c = nextc();
 	}
-	if (c != -1 && ISDIGIT(c)) {
+	if (c != -1 && (ISDIGIT(c) || !parser_is_identchar())) {
+	    pushback(c);
 	    if (tokidx == 1) {
 		compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c);
 	    }
@@ -7767,10 +7823,6 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/parse.y#L7823
 	    }
 	    return 0;
 	}
-	if (!parser_is_identchar()) {
-	    pushback(c);
-	    return '@';
-	}
 	break;
 
       case '_':
@@ -9669,22 +9721,17 @@ is_special_global_name(const char *m, co https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/parse.y#L9721
     int mb = 0;
 
     if (m >= e) return 0;
-    switch (*m) {
-      case '~': case '*': case '$': case '?': case '!': case '@':
-      case '/': case '\\': case ';': case ',': case '.': case '=':
-      case ':': case '<': case '>': case '\"':
-      case '&': case '`': case '\'': case '+':
-      case '0':
+    if (is_global_name_punct(*m)) {
 	++m;
-	break;
-      case '-':
+    }
+    else if (*m == '-') {
 	++m;
 	if (m < e && is_identchar(m, e, enc)) {
 	    if (!ISASCII(*m)) mb = 1;
 	    m += rb_enc_mbclen(m, e, enc);
 	}
-	break;
-      default:
+    }
+    else {
 	if (!rb_enc_isdigit(*m, enc)) return 0;
 	do {
 	    if (!ISASCII(*m)) mb = 1;
Index: ruby_1_9_3/version.h
===================================================================
--- ruby_1_9_3/version.h	(revision 42461)
+++ ruby_1_9_3/version.h	(revision 42462)
@@ -1,5 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/version.h#L1
 #define RUBY_VERSION "1.9.3"
-#define RUBY_PATCHLEVEL 462
+#define RUBY_PATCHLEVEL 463
 
 #define RUBY_RELEASE_DATE "2013-08-09"
 #define RUBY_RELEASE_YEAR 2013
Index: ruby_1_9_3/test/ruby/test_parse.rb
===================================================================
--- ruby_1_9_3/test/ruby/test_parse.rb	(revision 42461)
+++ ruby_1_9_3/test/ruby/test_parse.rb	(revision 42462)
@@ -358,6 +358,17 @@ class TestParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ruby/test_parse.rb#L358
     assert_equal("foo 1 bar", "foo #$1 bar")
   end
 
+  def test_dstr_disallowd_variable
+    bug8375 = '[ruby-core:54885] [Bug #8375]'
+    %w[@ @1 @@. @@ @@1 @@. $ $%].each do |src|
+      src = '#'+src+' '
+      str = assert_nothing_raised(SyntaxError, "#{bug8375} #{src.dump}") do
+        break eval('"'+src+'"')
+      end
+      assert_equal(src, str, bug8375)
+    end
+  end
+
   def test_dsym
     assert_nothing_raised { eval(':""') }
   end
@@ -524,13 +535,14 @@ class TestParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ruby/test_parse.rb#L535
       )
     end
 
-    assert_raise(SyntaxError) do
-      eval %q(
+    assert_nothing_raised(SyntaxError) do
+      x = eval %q(
 <<FOO
 #$
 FOO
       )
     end
+    assert_equal "\#$\n", x
 
     assert_raise(SyntaxError) do
       eval %Q(
@@ -550,14 +562,15 @@ FOO https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ruby/test_parse.rb#L562
       )
     end
 
-    assert_raise(SyntaxError) do
-      eval %q(
+    assert_nothing_raised(SyntaxError) do
+      x = eval %q(
 <<FOO
 #$
 foo
 FOO
       )
     end
+    assert_equal "\#$\nfoo\n", x
 
     assert_nothing_raised do
       eval "x = <<""FOO\r\n1\r\nFOO"
Index: ruby_1_9_3/test/ripper/test_parser_events.rb
===================================================================
--- ruby_1_9_3/test/ripper/test_parser_events.rb	(revision 42461)
+++ ruby_1_9_3/test/ripper/test_parser_events.rb	(revision 42462)
@@ -24,6 +24,10 @@ class TestRipper::ParserEvents < Test::U https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ripper/test_parser_events.rb#L24
     dp.parse.to_s
   end
 
+  def compile_error(str)
+    parse(str, :compile_error) {|e, msg| return msg}
+  end
+
   def test_program
     thru_program = false
     assert_equal '[void()]', parse('', :on_program) {thru_program = true}
@@ -1135,8 +1139,20 @@ class TestRipper::ParserEvents < Test::U https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ripper/test_parser_events.rb#L1139
   end
 
   def test_unterminated_regexp
-    compile_error = false
-    parse('/', :compile_error) {|e, msg| compile_error = msg}
-    assert_equal("unterminated regexp meets end of file", compile_error)
+    assert_equal("unterminated regexp meets end of file", compile_error('/'))
+  end
+
+  def test_invalid_instance_variable_name
+    assert_equal("`@1' is not allowed as an instance variable name", compile_error('@1'))
+    assert_equal("`@%' is not allowed as an instance variable name", compile_error('@%'))
+  end
+
+  def test_invalid_class_variable_name
+    assert_equal("`@@1' is not allowed as a class variable name", compile_error('@@1'))
+    assert_equal("`@@%' is not allowed as a class variable name", compile_error('@@%'))
+  end
+
+  def test_invalid_global_variable_name
+    assert_equal("`$%' is not allowed as a global variable name", compile_error('$%'))
   end
 end if ripper_test
Index: ruby_1_9_3/test/ripper/test_scanner_events.rb
===================================================================
--- ruby_1_9_3/test/ripper/test_scanner_events.rb	(revision 42461)
+++ ruby_1_9_3/test/ripper/test_scanner_events.rb	(revision 42462)
@@ -90,28 +90,28 @@ class TestRipper::ScannerEvents < Test:: https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ripper/test_scanner_events.rb#L90
   end
 
   def test_location
-    validate_location ""
-    validate_location " "
-    validate_location "@"
-    validate_location "\n"
-    validate_location "\r\n"
-    validate_location "\n\n\n\n\n\r\n\n\n"
-    validate_location "\n;\n;\n;\n;\n"
-    validate_location "nil"
-    validate_location "@ivar"
-    validate_location "1;2;3"
-    validate_location "1\n2\n3"
-    validate_location "1\n2\n3\n"
-    validate_location "def m(a) nil end"
-    validate_location "if true then false else nil end"
-    validate_location "BEGIN{print nil}"
-    validate_location "%w(a b\nc\r\nd \ne )"
-    validate_location %Q["a\nb\r\nc"]
-    validate_location "print(<<EOS)\nheredoc\nEOS\n"
-    validate_location "print(<<-\"EOS\")\nheredoc\n     EOS\n"
+    assert_location ""
+    assert_location " "
+    assert_location ":"
+    assert_location "\n"
+    assert_location "\r\n"
+    assert_location "\n\n\n\n\n\r\n\n\n"
+    assert_location "\n;\n;\n;\n;\n"
+    assert_location "nil"
+    assert_location "@ivar"
+    assert_location "1;2;3"
+    assert_location "1\n2\n3"
+    assert_location "1\n2\n3\n"
+    assert_location "def m(a) nil end"
+    assert_location "if true then false else nil end"
+    assert_location "BEGIN{print nil}"
+    assert_location "%w(a b\nc\r\nd \ne )"
+    assert_location %Q["a\nb\r\nc"]
+    assert_location "print(<<""EOS)\nheredoc\nEOS\n"
+    assert_location "print(<<-\"EOS\")\nheredoc\n     EOS\n"
   end
 
-  def validate_location(src)
+  def assert_location(src)
     buf = ''
     Ripper.lex(src).each do |pos, type, tok|
       line, col = *pos
@@ -823,8 +823,8 @@ class TestRipper::ScannerEvents < Test:: https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ripper/test_scanner_events.rb#L823
   def test_CHAR
     assert_equal [],
                  scan('CHAR', "")
-    assert_equal ["@"],
-                 scan('CHAR', "@")
+    assert_equal ["?a"],
+                 scan('CHAR', "?a")
     assert_equal [],
                  scan('CHAR', "@ivar")
   end

Property changes on: ruby_1_9_3
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r40606-40607,40635


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

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