Skip to content

Commit 8980207

Browse files
committed
[Bug #19882] Reject tokens invalid as symbols
1 parent 5a7f5bb commit 8980207

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

bootstraptest/test_syntax.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ def assert_syntax_error expected, code, message = ''
535535
assert_equal %q{[]}, %q{$&;[]}, '[ruby-dev:31068]'
536536
assert_syntax_error "syntax error, unexpected *, expecting '}'", %q{{*0}}, '[ruby-dev:31072]'
537537
assert_syntax_error "`@0' is not allowed as an instance variable name", %q{@0..0}, '[ruby-dev:31095]'
538-
assert_syntax_error "identifier $00 is not valid to get", %q{$00..0}, '[ruby-dev:31100]'
539-
assert_syntax_error "identifier $00 is not valid to set", %q{0..$00=1}
538+
assert_syntax_error "`$00' is not allowed as a global variable name", %q{$00..0}, '[ruby-dev:31100]'
539+
assert_syntax_error "`$00' is not allowed as a global variable name", %q{0..$00=1}
540540
assert_equal %q{0}, %q{[*0];0}, '[ruby-dev:31102]'
541541
assert_syntax_error "syntax error, unexpected ')'", %q{v0,(*,v1,) = 0}, '[ruby-dev:31104]'
542542
assert_equal %q{1}, %q{

parse.y

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ static void numparam_name(struct parser_params *p, ID id);
573573
#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
574574
#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
575575
#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
576+
#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
576577

577578
static st_table *
578579
push_pvtbl(struct parser_params *p)
@@ -9640,7 +9641,13 @@ parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
96409641

96419642
if (tokadd_ident(p, c)) return 0;
96429643
SET_LEX_STATE(EXPR_END);
9643-
tokenize_ident(p);
9644+
if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
9645+
tokenize_ident(p);
9646+
}
9647+
else {
9648+
compile_error(p, "`%.*s' is not allowed as a global variable name", toklen(p), tok(p));
9649+
set_yylval_noname();
9650+
}
96449651
return tGVAR;
96459652
}
96469653

@@ -13661,7 +13668,7 @@ rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, lo
1366113668
NODE *node, *succ;
1366213669

1366313670
if (!len) return ST_CONTINUE;
13664-
if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
13671+
if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
1366513672
return ST_CONTINUE;
1366613673

1366713674
var = intern_cstr(s, len, enc);

symbol.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,9 @@ is_global_name_punct(const int c)
109109
return (ruby_global_name_punct_bits[(c - 0x20) / 32] >> (c % 32)) & 1;
110110
}
111111

112-
int rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int allowed_attrset);
113-
114112
RUBY_SYMBOL_EXPORT_BEGIN
115113

114+
int rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int allowed_attrset);
116115
size_t rb_sym_immortal_count(void);
117116

118117
RUBY_SYMBOL_EXPORT_END

test/ruby/test_parse.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ def test_symbol
661661
assert_syntax_error(':@@1', /is not allowed/)
662662
assert_syntax_error(':@', /is not allowed/)
663663
assert_syntax_error(':@1', /is not allowed/)
664+
assert_syntax_error(':$01234', /is not allowed/)
664665
end
665666

666667
def test_parse_string
@@ -1478,8 +1479,8 @@ def set(arg)
14781479
end
14791480

14801481
def test_ungettable_gvar
1481-
assert_syntax_error('$01234', /not valid to get/)
1482-
assert_syntax_error('"#$01234"', /not valid to get/)
1482+
assert_syntax_error('$01234', /not allowed/)
1483+
assert_syntax_error('"#$01234"', /not allowed/)
14831484
end
14841485

14851486
=begin

0 commit comments

Comments
 (0)