ruby-changes:56310
From: Nobuyoshi <ko1@a...>
Date: Mon, 1 Jul 2019 14:00:44 +0900 (JST)
Subject: [ruby-changes:56310] Nobuyoshi Nakada: 99dc885974 (master): Fixed inadvertent ID creation in rb_iv_get
https://git.ruby-lang.org/ruby.git/commit/?id=99dc885974 From 99dc885974bfe637f3e74f52efdbbf77d66d0d68 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Mon, 1 Jul 2019 13:56:55 +0900 Subject: Fixed inadvertent ID creation in rb_iv_get diff --git a/ext/-test-/symbol/init.c b/ext/-test-/symbol/init.c index 20cf2fa..4038701 100644 --- a/ext/-test-/symbol/init.c +++ b/ext/-test-/symbol/init.c @@ -20,6 +20,13 @@ sym_pinneddown_p(VALUE dummy, VALUE sym) https://github.com/ruby/ruby/blob/trunk/ext/-test-/symbol/init.c#L20 #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) https://github.com/ruby/ruby/blob/trunk/ext/-test-/symbol/init.c#L34 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 b304f09..40b3f59 100644 --- a/test/-ext-/symbol/test_inadvertent_creation.rb +++ b/test/-ext-/symbol/test_inadvertent_creation.rb @@ -482,5 +482,14 @@ module Test_Symbol https://github.com/ruby/ruby/blob/trunk/test/-ext-/symbol/test_inadvertent_creation.rb#L482 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 a691098..b902fef 100644 --- a/variable.c +++ b/variable.c @@ -3361,8 +3361,13 @@ rb_mod_remove_cvar(VALUE mod, VALUE name) https://github.com/ruby/ruby/blob/trunk/variable.c#L3361 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); } -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/