From f0181f1e0719045fccfa5dd9224275c6a4d51e5b Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 22 Jul 2011 12:39:06 -0700 Subject: [PATCH] Remove more inadvertent symbol creation This removes inadvertent symbol creation for the following methods: class_variable_get instance_variable_get remove_class_variable remove_instance_variable remove_const method instance_method public_method public_instance_method It also fixes the NameError messages for the following cases: Object.const_defined?("gadzooks1") Object.instance_variable_defined?("gadzooks2") Object.class_variable_defined?("gadzooks3") One new function is added to intern.h, rb_name_error_str, which is just like rb_name_error except it takes a VALUE instead of an ID. --- error.c | 15 ++++++++++++++ include/ruby/intern.h | 1 + object.c | 44 ++++++++++++++++++++++++++++++++++++----- proc.c | 48 ++++++++++++++++++++++++++++++++++++++++++--- test/ruby/test_symbol.rb | 25 +++++++++++++++++++++++ variable.c | 29 ++++++++++++++++++++++++-- 6 files changed, 149 insertions(+), 13 deletions(-) diff --git a/error.c b/error.c index 42f8368..834eede 100644 --- a/error.c +++ b/error.c @@ -812,6 +812,21 @@ rb_name_error(ID id, const char *fmt, ...) rb_exc_raise(exc); } +void +rb_name_error_str(VALUE str, const char *fmt, ...) +{ + VALUE exc, argv[2]; + va_list args; + + va_start(args, fmt); + argv[0] = rb_vsprintf(fmt, args); + va_end(args); + + argv[1] = str; + exc = rb_class_new_instance(2, argv, rb_eNameError); + rb_exc_raise(exc); +} + /* * call-seq: * NameError.new(msg [, name]) -> name_error diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 71896ce..27db84e 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -209,6 +209,7 @@ VALUE rb_exc_new2(VALUE, const char*); VALUE rb_exc_new3(VALUE, VALUE); PRINTF_ARGS(NORETURN(void rb_loaderror(const char*, ...)), 1, 2); PRINTF_ARGS(NORETURN(void rb_name_error(ID, const char*, ...)), 2, 3); +PRINTF_ARGS(NORETURN(void rb_name_error_str(VALUE, const char*, ...)), 2, 3); NORETURN(void rb_invalid_str(const char*, const char*)); PRINTF_ARGS(void rb_compile_error(const char*, int, const char*, ...), 3, 4); PRINTF_ARGS(void rb_compile_error_with_enc(const char*, int, void *, const char*, ...), 4, 5); diff --git a/object.c b/object.c index 42d9d25..f325213 100644 --- a/object.c +++ b/object.c @@ -1833,8 +1833,13 @@ rb_mod_const_defined(int argc, VALUE *argv, VALUE mod) else { rb_scan_args(argc, argv, "11", &name, &recur); } - if (!(id = rb_check_id(name)) && rb_is_const_name(name)) - return Qfalse; + if (!(id = rb_check_id(name))) { + if(rb_is_const_name(name)) { + return Qfalse; + } else { + rb_name_error_str(name, "wrong constant name %s", RSTRING_PTR(name)); + } + } if (!rb_is_const_id(id)) { rb_name_error(id, "wrong constant name %s", rb_id2name(id)); } @@ -1864,8 +1869,15 @@ rb_mod_const_defined(int argc, VALUE *argv, VALUE mod) static VALUE rb_obj_ivar_get(VALUE obj, VALUE iv) { - ID id = rb_to_id(iv); + ID id = rb_check_id(iv); + if (!id) { + if(rb_is_instance_name(iv)) { + return Qnil; + } else { + rb_name_error_str(iv, "`%s' is not allowed as an instance variable name", RSTRING_PTR(iv)); + } + } if (!rb_is_instance_id(id)) { rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id)); } @@ -1926,7 +1938,13 @@ rb_obj_ivar_defined(VALUE obj, VALUE iv) { ID id = rb_check_id(iv); - if (!id && rb_is_instance_name(iv)) return Qfalse; + if (!id) { + if(rb_is_instance_name(iv)) { + return Qfalse; + } else { + rb_name_error_str(iv, "`%s' is not allowed as an instance variable name", RSTRING_PTR(iv)); + } + } if (!rb_is_instance_id(id)) { rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id)); } @@ -1950,8 +1968,16 @@ rb_obj_ivar_defined(VALUE obj, VALUE iv) static VALUE rb_mod_cvar_get(VALUE obj, VALUE iv) { - ID id = rb_to_id(iv); + ID id = rb_check_id(iv); + if (!id) { + if(rb_is_class_name(iv)) { + rb_name_error_str(iv, "uninitialized class variable %s in %s", + RSTRING_PTR(iv), rb_class2name(obj)); + } else { + rb_name_error_str(iv, "`%s' is not allowed as a class variable name", RSTRING_PTR(iv)); + } + } if (!rb_is_class_id(id)) { rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id)); } @@ -2006,7 +2032,13 @@ rb_mod_cvar_defined(VALUE obj, VALUE iv) { ID id = rb_check_id(iv); - if (!id && rb_is_class_name(iv)) return Qfalse; + if (!id) { + if(rb_is_class_name(iv)) { + return Qfalse; + } else { + rb_name_error_str(iv, "`%s' is not allowed as a class variable name", RSTRING_PTR(iv)); + } + } if (!rb_is_class_id(id)) { rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id)); } diff --git a/proc.c b/proc.c index d8077e7..000355b 100644 --- a/proc.c +++ b/proc.c @@ -28,6 +28,7 @@ VALUE rb_cProc; static VALUE bmcall(VALUE, VALUE); static int method_arity(VALUE); +static ID attached; /* Proc */ @@ -1139,6 +1140,28 @@ method_owner(VALUE obj) return data->me.klass; } +static void +rb_method_name_error(VALUE klass, VALUE str) { + const char *s0 = " class"; + VALUE c = klass; + + if (FL_TEST(c, FL_SINGLETON)) { + VALUE obj = rb_ivar_get(klass, attached); + + switch (TYPE(obj)) { + case T_MODULE: + case T_CLASS: + c = obj; + s0 = ""; + } + } + else if (TYPE(c) == T_MODULE) { + s0 = " module"; + } + rb_name_error_str(str, "undefined method `%s' for%s `%s'", + RSTRING_PTR(str), s0, rb_class2name(c)); +} + /* * call-seq: * obj.method(sym) -> method @@ -1170,7 +1193,11 @@ method_owner(VALUE obj) VALUE rb_obj_method(VALUE obj, VALUE vid) { - return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod, FALSE); + ID id = rb_check_id(vid); + if (!id) { + rb_method_name_error(CLASS_OF(obj), vid); + } + return mnew(CLASS_OF(obj), obj, id, rb_cMethod, FALSE); } /* @@ -1183,7 +1210,11 @@ rb_obj_method(VALUE obj, VALUE vid) VALUE rb_obj_public_method(VALUE obj, VALUE vid) { - return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod, TRUE); + ID id = rb_check_id(vid); + if (!id) { + rb_method_name_error(CLASS_OF(obj), vid); + } + return mnew(CLASS_OF(obj), obj, id, rb_cMethod, TRUE); } /* @@ -1220,7 +1251,11 @@ rb_obj_public_method(VALUE obj, VALUE vid) static VALUE rb_mod_instance_method(VALUE mod, VALUE vid) { - return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, FALSE); + ID id = rb_check_id(vid); + if (!id) { + rb_method_name_error(mod, vid); + } + return mnew(mod, Qundef, id, rb_cUnboundMethod, FALSE); } /* @@ -1233,7 +1268,11 @@ rb_mod_instance_method(VALUE mod, VALUE vid) static VALUE rb_mod_public_instance_method(VALUE mod, VALUE vid) { - return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, TRUE); + ID id = rb_check_id(vid); + if (!id) { + rb_method_name_error(mod, vid); + } + return mnew(mod, Qundef, id, rb_cUnboundMethod, TRUE); } /* @@ -2234,5 +2273,6 @@ Init_Binding(void) rb_define_method(rb_cBinding, "dup", binding_dup, 0); rb_define_method(rb_cBinding, "eval", bind_eval, -1); rb_define_global_function("binding", rb_f_binding, 0); + attached = rb_intern("__attached__"); } diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb index 282964c..6f385bc 100644 --- a/test/ruby/test_symbol.rb +++ b/test/ruby/test_symbol.rb @@ -159,4 +159,29 @@ class TestSymbol < Test::Unit::TestCase assert !Symbol.all_symbols.any? {|sym| sym.to_s == str}, msg end end + + def test_no_inadvertent_symbol_creation2 + feature50XX = '[ruby-core:383XX]' + c = Class.new + s = "gadzoooks" + {:instance_variable_get => ["@#{s}1", nil], + :class_variable_get => ["@@#{s}1", NameError], + :remove_instance_variable => ["@#{s}2", NameError], + :remove_class_variable => ["@@#{s}2", NameError], + :remove_const => ["A#{s}", NameError], + :method => ["#{s}1", NameError], + :public_method => ["#{s}2", NameError], + :instance_method => ["#{s}3", NameError], + :public_instance_method => ["#{s}4", NameError], + }.each do |meth, arr| + str, ret = arr + msg = "#{meth}(#{str}) #{feature50XX}" + if ret.is_a?(Class) && (ret < Exception) + assert_raises(ret){c.send(meth, str)} + else + assert(c.send(meth, str) == ret, msg) + end + assert !Symbol.all_symbols.any? {|sym| sym.to_s == str}, msg + end + end end diff --git a/variable.c b/variable.c index 251fb3b..3940fd1 100644 --- a/variable.c +++ b/variable.c @@ -1299,7 +1299,7 @@ VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name) { VALUE val = Qnil; - const ID id = rb_to_id(name); + const ID id = rb_check_id(name); st_data_t n, v; struct st_table *iv_index_tbl; st_data_t index; @@ -1307,6 +1307,13 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name) if (!OBJ_UNTRUSTED(obj) && rb_safe_level() >= 4) rb_raise(rb_eSecurityError, "Insecure: can't modify instance variable"); rb_check_frozen(obj); + if (!id) { + if (rb_is_instance_name(name)) { + rb_name_error_str(name, "instance variable %s not defined", RSTRING_PTR(name)); + } else { + rb_name_error_str(name, "`%s' is not allowed as an instance variable name", RSTRING_PTR(name)); + } + } if (!rb_is_instance_id(id)) { rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id)); } @@ -1677,8 +1684,16 @@ rb_public_const_get_at(VALUE klass, ID id) VALUE rb_mod_remove_const(VALUE mod, VALUE name) { - const ID id = rb_to_id(name); + const ID id = rb_check_id(name); + if (!id) { + if (rb_is_const_name(name)) { + rb_name_error_str(name, "constant %s::%s not defined", + rb_class2name(mod), RSTRING_PTR(name)); + } else { + rb_name_error_str(name, "`%s' is not allowed as a constant name", RSTRING_PTR(name)); + } + } if (!rb_is_const_id(id)) { rb_name_error(id, "`%s' is not allowed as a constant name", rb_id2name(id)); } @@ -2189,9 +2204,17 @@ rb_mod_class_variables(VALUE obj) VALUE rb_mod_remove_cvar(VALUE mod, VALUE name) { - const ID id = rb_to_id(name); + const ID id = rb_check_id(name); st_data_t val, n = id; + if (!id) { + if (rb_is_class_name(name)) { + rb_name_error_str(name, "class variable %s not defined for %s", + RSTRING_PTR(name), rb_class2name(mod)); + } else { + rb_name_error_str(name, "wrong class variable name %s", RSTRING_PTR(name)); + } + } if (!rb_is_class_id(id)) { rb_name_error(id, "wrong class variable name %s", rb_id2name(id)); } -- 1.7.5