diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-10-09 14:57:04 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-10-09 14:57:04 +0000 |
commit | 24cf72029f0c43f59520b88d1eafee4959bcddc1 (patch) | |
tree | 5a95f9566d6389ff96fa1e8cb283fcb64ec7de67 | |
parent | 372eb2cbfed1b7f5d7e2af1ced551ab4087c95a5 (diff) |
object.c: avoid inadvertent symbol creation
* object.c (id_for_attr): avoid inadvertent symbol creation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43219 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | object.c | 37 | ||||
-rw-r--r-- | test/-ext-/symbol/test_inadvertent_creation.rb | 28 | ||||
-rw-r--r-- | vm_method.c | 7 |
4 files changed, 68 insertions, 8 deletions
@@ -1,3 +1,7 @@ +Wed Oct 9 23:57:02 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * object.c (id_for_attr): avoid inadvertent symbol creation. + Wed Oct 9 18:03:01 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> * vm_method.c (rb_attr): preserve encoding of the attribute ID in @@ -1889,6 +1889,35 @@ check_setter_id(VALUE name, int (*valid_id_p)(ID), int (*valid_name_p)(VALUE), return id; } +static int +rb_is_attr_id(ID id) +{ + return rb_is_local_id(id) || rb_is_const_id(id); +} + +static int +rb_is_attr_name(VALUE name) +{ + return rb_is_local_name(name) || rb_is_const_name(name); +} + +static const char invalid_attribute_name[] = "invalid attribute name `%"PRIsVALUE"'"; + +static ID +id_for_attr(VALUE name) +{ + return id_for_setter(name, attr, invalid_attribute_name); +} + +ID +rb_check_attr_id(ID id) +{ + if (!rb_is_attr_id(id)) { + rb_name_error_str(id, invalid_attribute_name, QUOTE_ID(id)); + } + return id; +} + /* * call-seq: * attr_reader(symbol, ...) -> nil @@ -1908,7 +1937,7 @@ rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass) int i; for (i=0; i<argc; i++) { - rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE); + rb_attr(klass, id_for_attr(argv[i]), TRUE, FALSE, TRUE); } return Qnil; } @@ -1918,7 +1947,7 @@ rb_mod_attr(int argc, VALUE *argv, VALUE klass) { if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) { rb_warning("optional boolean argument is obsoleted"); - rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE); + rb_attr(klass, id_for_attr(argv[0]), 1, RTEST(argv[1]), TRUE); return Qnil; } return rb_mod_attr_reader(argc, argv, klass); @@ -1940,7 +1969,7 @@ rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass) int i; for (i=0; i<argc; i++) { - rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE); + rb_attr(klass, id_for_attr(argv[i]), FALSE, TRUE, TRUE); } return Qnil; } @@ -1968,7 +1997,7 @@ rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass) int i; for (i=0; i<argc; i++) { - rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE); + rb_attr(klass, id_for_attr(argv[i]), TRUE, TRUE, TRUE); } return Qnil; } diff --git a/test/-ext-/symbol/test_inadvertent_creation.rb b/test/-ext-/symbol/test_inadvertent_creation.rb index ba5a162231..82a64034b1 100644 --- a/test/-ext-/symbol/test_inadvertent_creation.rb +++ b/test/-ext-/symbol/test_inadvertent_creation.rb @@ -234,5 +234,33 @@ module Test_Symbol assert_raise(NameError) {s[name] = true} assert_not_interned(name) end + + def test_invalid_attr + name = noninterned_name("*") + mod = Module.new + assert_raise(NameError) {mod.module_eval {attr(name)}} + assert_not_interned(name) + end + + def test_invalid_attr_reader + name = noninterned_name("*") + mod = Module.new + assert_raise(NameError) {mod.module_eval {attr_reader(name)}} + assert_not_interned(name) + end + + def test_invalid_attr_writer + name = noninterned_name("*") + mod = Module.new + assert_raise(NameError) {mod.module_eval {attr_writer(name)}} + assert_not_interned(name) + end + + def test_invalid_attr_accessor + name = noninterned_name("*") + mod = Module.new + assert_raise(NameError) {mod.module_eval {attr_accessor(name)}} + assert_not_interned(name) + end end end diff --git a/vm_method.c b/vm_method.c index 584982d51d..0066c17c8a 100644 --- a/vm_method.c +++ b/vm_method.c @@ -809,6 +809,8 @@ rb_method_boundp(VALUE klass, ID id, int ex) return 0; } +extern ID rb_check_attr_id(ID id); + void rb_attr(VALUE klass, ID id, int read, int write, int ex) { @@ -834,10 +836,7 @@ rb_attr(VALUE klass, ID id, int read, int write, int ex) } } - if (!rb_is_local_id(id) && !rb_is_const_id(id)) { - rb_name_error_str(id, "invalid attribute name `%"PRIsVALUE"'", QUOTE_ID(id)); - } - aname = rb_id2str(id); + aname = rb_id2str(rb_check_attr_id(id)); if (NIL_P(aname)) { rb_raise(rb_eArgError, "argument needs to be symbol or string"); } |