From f0483c494fe092a35ba2022702c554eac68efddf Mon Sep 17 00:00:00 2001 From: mame Date: Fri, 28 Jan 2011 17:57:27 +0000 Subject: * constant.h, variable.c: to ensure compatibility, rb_const_get_* must not raise an exception even when the constant is private. Instead, rb_public_const_get_* and rb_public_const_defined_* are introduced, which raise an exception when the referring constant is private. see [ruby-core:32912]. * vm_insnhelper.c (vm_get_ev_const): use rb_public_const_get_* instead of rb_const_get_* to follow the constant visibility when user code refers a constant. * test/ruby/test_marshal.rb (test_marshal_private_class): add a test. This test had failed because of incompatibility of rb_const_get. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30713 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 15 ++++++++++++ constant.h | 6 +++++ test/ruby/test_marshal.rb | 14 +++++++++++ variable.c | 60 +++++++++++++++++++++++++++++++++++++++-------- vm_insnhelper.c | 8 +++---- 5 files changed, 89 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index b2c3658670..b7829c5ef7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +Sat Jan 29 01:19:17 2011 Yusuke Endoh + + * constant.h, variable.c: to ensure compatibility, rb_const_get_* must + not raise an exception even when the constant is private. Instead, + rb_public_const_get_* and rb_public_const_defined_* are introduced, + which raise an exception when the referring constant is private. + see [ruby-core:32912]. + + * vm_insnhelper.c (vm_get_ev_const): use rb_public_const_get_* instead + of rb_const_get_* to follow the constant visibility when user code + refers a constant. + + * test/ruby/test_marshal.rb (test_marshal_private_class): add a test. + This test had failed because of incompatibility of rb_const_get. + Sat Jan 29 00:30:44 2011 Yusuke Endoh * variable.c (set_const_visibility): fix typo. a patch from Tomoyuki diff --git a/constant.h b/constant.h index ad9ef5a065..8232910737 100644 --- a/constant.h +++ b/constant.h @@ -24,5 +24,11 @@ typedef struct rb_const_entry_struct { VALUE rb_mod_private_constant(int argc, VALUE *argv, VALUE obj); VALUE rb_mod_public_constant(int argc, VALUE *argv, VALUE obj); void rb_free_const_table(st_table *tbl); +VALUE rb_public_const_get(VALUE klass, ID id); +VALUE rb_public_const_get_at(VALUE klass, ID id); +VALUE rb_public_const_get_from(VALUE klass, ID id); +int rb_public_const_defined(VALUE klass, ID id); +int rb_public_const_defined_at(VALUE klass, ID id); +int rb_public_const_defined_from(VALUE klass, ID id); #endif /* CONSTANT_H */ diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb index f3d86277dd..e1dc7675ef 100644 --- a/test/ruby/test_marshal.rb +++ b/test/ruby/test_marshal.rb @@ -454,5 +454,19 @@ class TestMarshal < Test::Unit::TestCase o2 = Marshal.load(m) assert_equal(o1, o2) end + + class PrivateClass + def initialize(foo) + @foo = foo + end + attr_reader :foo + end + private_constant :PrivateClass + def test_marshal_private_class + o1 = PrivateClass.new("test") + o2 = Marshal.load(Marshal.dump(o1)) + assert_equal(o1.class, o2.class) + assert_equal(o1.foo, o2.foo) + end end diff --git a/variable.c b/variable.c index f737973878..1f4cf76fae 100644 --- a/variable.c +++ b/variable.c @@ -1575,7 +1575,7 @@ rb_autoload_p(VALUE mod, ID id) } static VALUE -rb_const_get_0(VALUE klass, ID id, int exclude, int recurse) +rb_const_get_0(VALUE klass, ID id, int exclude, int recurse, int visibility) { VALUE value, tmp; int mod_retry = 0; @@ -1587,7 +1587,7 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse) st_data_t data; while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &data)) { rb_const_entry_t *ce = (rb_const_entry_t *)data; - if (ce->flag == CONST_PRIVATE) { + if (visibility && ce->flag == CONST_PRIVATE) { rb_name_error(id, "private constant %s::%s referenced", rb_class2name(klass), rb_id2name(id)); } value = ce->value; @@ -1620,19 +1620,37 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse) VALUE rb_const_get_from(VALUE klass, ID id) { - return rb_const_get_0(klass, id, TRUE, TRUE); + return rb_const_get_0(klass, id, TRUE, TRUE, FALSE); } VALUE rb_const_get(VALUE klass, ID id) { - return rb_const_get_0(klass, id, FALSE, TRUE); + return rb_const_get_0(klass, id, FALSE, TRUE, FALSE); } VALUE rb_const_get_at(VALUE klass, ID id) { - return rb_const_get_0(klass, id, TRUE, FALSE); + return rb_const_get_0(klass, id, TRUE, FALSE, FALSE); +} + +VALUE +rb_public_const_get_from(VALUE klass, ID id) +{ + return rb_const_get_0(klass, id, TRUE, TRUE, TRUE); +} + +VALUE +rb_public_const_get(VALUE klass, ID id) +{ + return rb_const_get_0(klass, id, FALSE, TRUE, TRUE); +} + +VALUE +rb_public_const_get_at(VALUE klass, ID id) +{ + return rb_const_get_0(klass, id, TRUE, FALSE, TRUE); } /* @@ -1781,7 +1799,7 @@ rb_mod_constants(int argc, VALUE *argv, VALUE mod) } static int -rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse) +rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse, int visibility) { st_data_t value; VALUE tmp; @@ -1791,7 +1809,11 @@ rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse) retry: while (tmp) { if (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &value)) { - if (((rb_const_entry_t*)value)->value == Qundef && !autoload_node((VALUE)klass, id, 0)) + rb_const_entry_t *ce = (rb_const_entry_t *)value; + if (visibility && ce->flag == CONST_PRIVATE) { + return (int)Qfalse; + } + if (ce->value == Qundef && !autoload_node((VALUE)klass, id, 0)) return (int)Qfalse; return (int)Qtrue; } @@ -1809,19 +1831,37 @@ rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse) int rb_const_defined_from(VALUE klass, ID id) { - return rb_const_defined_0(klass, id, TRUE, TRUE); + return rb_const_defined_0(klass, id, TRUE, TRUE, FALSE); } int rb_const_defined(VALUE klass, ID id) { - return rb_const_defined_0(klass, id, FALSE, TRUE); + return rb_const_defined_0(klass, id, FALSE, TRUE, FALSE); } int rb_const_defined_at(VALUE klass, ID id) { - return rb_const_defined_0(klass, id, TRUE, FALSE); + return rb_const_defined_0(klass, id, TRUE, FALSE, FALSE); +} + +int +rb_public_const_defined_from(VALUE klass, ID id) +{ + return rb_const_defined_0(klass, id, TRUE, TRUE, TRUE); +} + +int +rb_public_const_defined(VALUE klass, ID id) +{ + return rb_const_defined_0(klass, id, FALSE, TRUE, TRUE); +} + +int +rb_public_const_defined_at(VALUE klass, ID id) +{ + return rb_const_defined_0(klass, id, TRUE, FALSE, TRUE); } void diff --git a/vm_insnhelper.c b/vm_insnhelper.c index d537661204..94d6a87920 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1199,19 +1199,19 @@ vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq, } if (is_defined) { - return rb_const_defined(klass, id); + return rb_public_const_defined(klass, id); } else { - return rb_const_get(klass, id); + return rb_public_const_get(klass, id); } } else { vm_check_if_namespace(orig_klass); if (is_defined) { - return rb_const_defined_from(orig_klass, id); + return rb_public_const_defined_from(orig_klass, id); } else { - return rb_const_get_from(orig_klass, id); + return rb_public_const_get_from(orig_klass, id); } } } -- cgit v1.2.3