Skip to content
Browse files

* 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
  • Loading branch information...
1 parent a79ff4c commit f0483c494fe092a35ba2022702c554eac68efddf @mame mame committed Jan 28, 2011
Showing with 89 additions and 14 deletions.
  1. +15 −0 ChangeLog
  2. +6 −0 constant.h
  3. +14 −0 test/ruby/test_marshal.rb
  4. +50 −10 variable.c
  5. +4 −4 vm_insnhelper.c
View
15 ChangeLog
@@ -1,3 +1,18 @@
+Sat Jan 29 01:19:17 2011 Yusuke Endoh <mame@tsg.ne.jp>
+
+ * 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 <mame@tsg.ne.jp>
* variable.c (set_const_visibility): fix typo. a patch from Tomoyuki
View
6 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 */
View
14 test/ruby/test_marshal.rb
@@ -454,5 +454,19 @@ def test_marshal_encoding_encoding
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
View
60 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
View
8 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);
}
}
}

0 comments on commit f0483c4

Please sign in to comment.
Something went wrong with that request. Please try again.