From 8f1759143533d2b772efd5184ba02738f66fe1fc Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jul 2022 20:24:26 +0900 Subject: [PATCH] [Bug #18905] Check symbol name types more strictly --- symbol.c | 2 +- test/-ext-/symbol/test_type.rb | 9 ++++----- test/ruby/test_symbol.rb | 13 +++++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/symbol.c b/symbol.c index f5e876c7d7..dc7d72cb90 100644 --- a/symbol.c +++ b/symbol.c @@ -378,7 +378,7 @@ rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int a if (m >= e || (*m != '_' && !ISALPHA(*m) && ISASCII(*m))) { if (len > 1 && *(e-1) == '=') { type = rb_enc_symname_type(name, len-1, enc, allowed_attrset); - if (type != ID_ATTRSET) return ID_ATTRSET; + if (allowed_attrset & (1U << type)) return ID_ATTRSET; } return -1; } diff --git a/test/-ext-/symbol/test_type.rb b/test/-ext-/symbol/test_type.rb index 7cb3c7b6ef..fdee692fe4 100644 --- a/test/-ext-/symbol/test_type.rb +++ b/test/-ext-/symbol/test_type.rb @@ -93,16 +93,16 @@ module Test_Symbol assert_symtype("@foo=", :attrset?) assert_symtype("@@foo=", :attrset?) assert_symtype("$foo=", :attrset?) - assert_symtype("0=", :attrset?) - assert_symtype("@=", :attrset?) - assert_symtype("@@=", :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?) - assert_symtype("[foo]=", :attrset?) + assert_not_symtype("[foo]=", :attrset?) assert_equal(:"foo=", Bug::Symbol.attrset("foo")) assert_symtype(Bug::Symbol.attrset("foo"), :attrset?) assert_equal(:"Foo=", Bug::Symbol.attrset("Foo")) @@ -114,7 +114,6 @@ module Test_Symbol assert_equal(:"$foo=", Bug::Symbol.attrset("$foo")) assert_symtype(Bug::Symbol.attrset("$foo"), :attrset?) assert_equal(:"[foo]=", Bug::Symbol.attrset("[foo]")) - assert_symtype(Bug::Symbol.attrset("[foo]"), :attrset?) assert_equal(:[]=, Bug::Symbol.attrset(:[])) assert_symtype(Bug::Symbol.attrset("foo?="), :attrset?) assert_equal(:"foo?=", Bug::Symbol.attrset(:foo?)) diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb index f7f17b8d67..1d2a18d734 100644 --- a/test/ruby/test_symbol.rb +++ b/test/ruby/test_symbol.rb @@ -36,6 +36,19 @@ class TestSymbol < Test::Unit::TestCase assert_eval_inspected(:"@@1", false) assert_eval_inspected(:"@", false) assert_eval_inspected(:"@@", false) + assert_eval_inspected(:"[]=") + assert_eval_inspected(:"[][]", false) + assert_eval_inspected(:"[][]=", false) + assert_eval_inspected(:"@=", false) + assert_eval_inspected(:"@@=", false) + assert_eval_inspected(:"@x=", false) + assert_eval_inspected(:"@@x=", false) + assert_eval_inspected(:"$$=", false) + assert_eval_inspected(:"$==", false) + assert_eval_inspected(:"$x=", false) + assert_eval_inspected(:"$$$=", false) + assert_eval_inspected(:"foo?=", false) + assert_eval_inspected(:"foo!=", false) end def assert_inspect_evaled(n)