diff --git a/ext/-test-/symbol/init.c b/ext/-test-/symbol/init.c index 20cf2fa079..4038701ac9 100644 --- a/ext/-test-/symbol/init.c +++ b/ext/-test-/symbol/init.c @@ -20,6 +20,13 @@ sym_pinneddown_p(VALUE dummy, VALUE sym) #endif } +static VALUE +sym_iv_get(VALUE dummy, VALUE obj, VALUE name) +{ + const char *n = StringValueCStr(name); + return rb_iv_get(obj, n); +} + void Init_symbol(void) { @@ -27,5 +34,6 @@ Init_symbol(void) VALUE klass = rb_define_class_under(mBug, "Symbol", rb_cSymbol); rb_define_singleton_method(klass, "find", sym_find, 1); rb_define_singleton_method(klass, "pinneddown?", sym_pinneddown_p, 1); + rb_define_singleton_method(klass, "iv_get", sym_iv_get, 2); TEST_INIT_FUNCS(init); } diff --git a/test/-ext-/symbol/test_inadvertent_creation.rb b/test/-ext-/symbol/test_inadvertent_creation.rb index b304f09933..40b3f59f7f 100644 --- a/test/-ext-/symbol/test_inadvertent_creation.rb +++ b/test/-ext-/symbol/test_inadvertent_creation.rb @@ -482,5 +482,14 @@ module Test_Symbol foo.call(name.to_sym => 42) end end + + def test_iv_get + obj = Object.new + assert_warning(/not initialized/) do + assert_no_immortal_symbol_created("rb_iv_get") do |name| + Bug::Symbol.iv_get(obj, name) + end + end + end end end diff --git a/variable.c b/variable.c index a6910985fa..b902fef003 100644 --- a/variable.c +++ b/variable.c @@ -3361,8 +3361,13 @@ rb_mod_remove_cvar(VALUE mod, VALUE name) VALUE rb_iv_get(VALUE obj, const char *name) { - ID id = rb_intern(name); + ID id = rb_check_id_cstr(name, strlen(name), rb_usascii_encoding()); + if (!id) { + if (RTEST(ruby_verbose)) + rb_warning("instance variable %s not initialized", name); + return Qnil; + } return rb_ivar_get(obj, id); }