1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

parse.y: fail if invalid name

* parse.y (parser_yylex): fail if $, @, @@ are not followed by a valid
  name character.  [ruby-core:54846] [Bug #8375].

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40607 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-05-08 04:07:22 +00:00
parent 4dfd373905
commit 85280f4b55
4 changed files with 31 additions and 12 deletions

View file

@ -1,3 +1,8 @@
Wed May 8 13:07:17 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (parser_yylex): fail if $, @, @@ are not followed by a valid
name character. [ruby-core:54846] [Bug #8375].
Wed May 8 13:06:31 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> Wed May 8 13:06:31 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* include/ruby/ruby.h (ISGRAPH): add missing macro. * include/ruby/ruby.h (ISGRAPH): add missing macro.

10
parse.y
View file

@ -7876,7 +7876,8 @@ parser_yylex(struct parser_params *parser)
default: default:
if (!parser_is_identchar()) { if (!parser_is_identchar()) {
pushback(c); pushback(c);
return '$'; compile_error(PARSER_ARG "`$%c' is not allowed as a global variable name", c);
return 0;
} }
case '0': case '0':
tokadd('$'); tokadd('$');
@ -7891,7 +7892,8 @@ parser_yylex(struct parser_params *parser)
tokadd('@'); tokadd('@');
c = nextc(); c = nextc();
} }
if (c != -1 && ISDIGIT(c)) { if (c != -1 && (ISDIGIT(c) || !parser_is_identchar())) {
pushback(c);
if (tokidx == 1) { if (tokidx == 1) {
compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c); compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c);
} }
@ -7900,10 +7902,6 @@ parser_yylex(struct parser_params *parser)
} }
return 0; return 0;
} }
if (!parser_is_identchar()) {
pushback(c);
return '@';
}
break; break;
case '_': case '_':

View file

@ -24,6 +24,10 @@ class TestRipper::ParserEvents < Test::Unit::TestCase
dp.parse.to_s dp.parse.to_s
end end
def compile_error(str)
parse(str, :compile_error) {|e, msg| return msg}
end
def test_program def test_program
thru_program = false thru_program = false
assert_equal '[void()]', parse('', :on_program) {thru_program = true} assert_equal '[void()]', parse('', :on_program) {thru_program = true}
@ -1167,8 +1171,20 @@ class TestRipper::ParserEvents < Test::Unit::TestCase
end end
def test_unterminated_regexp def test_unterminated_regexp
compile_error = false assert_equal("unterminated regexp meets end of file", compile_error('/'))
parse('/', :compile_error) {|e, msg| compile_error = msg} end
assert_equal("unterminated regexp meets end of file", compile_error)
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
end if ripper_test end if ripper_test

View file

@ -92,7 +92,7 @@ class TestRipper::ScannerEvents < Test::Unit::TestCase
def test_location def test_location
assert_location "" assert_location ""
assert_location " " assert_location " "
assert_location "@" assert_location ":"
assert_location "\n" assert_location "\n"
assert_location "\r\n" assert_location "\r\n"
assert_location "\n\n\n\n\n\r\n\n\n" assert_location "\n\n\n\n\n\r\n\n\n"
@ -842,8 +842,8 @@ class TestRipper::ScannerEvents < Test::Unit::TestCase
def test_CHAR def test_CHAR
assert_equal [], assert_equal [],
scan('CHAR', "") scan('CHAR', "")
assert_equal ["@"], assert_equal ["?a"],
scan('CHAR', "@") scan('CHAR', "?a")
assert_equal [], assert_equal [],
scan('CHAR', "@ivar") scan('CHAR', "@ivar")
end end