Skip to content

Commit fca2e6f

Browse files
byrootetiennebarrie
andcommitted
variable.c: Fix rb_ivar_foreach to not yield object_id of complex objects
Co-Authored-By: Étienne Barrié <[email protected]>
1 parent bf2c8ad commit fca2e6f

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

test/ruby/test_marshal.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,30 @@ def test_marshal_private_class
469469
assert_equal(o1.foo, o2.foo)
470470
end
471471

472+
class TooComplex
473+
def initialize
474+
@marshal_too_complex = 1
475+
end
476+
end
477+
478+
def test_complex_shape_object_id_not_dumped
479+
if defined?(RubyVM::Shape::SHAPE_MAX_VARIATIONS)
480+
assert_equal 8, RubyVM::Shape::SHAPE_MAX_VARIATIONS
481+
end
482+
8.times do |i|
483+
TooComplex.new.instance_variable_set("@TestObjectIdTooComplex#{i}", 1)
484+
end
485+
obj = TooComplex.new
486+
ivar = "@a#{rand(10_000).to_s.rjust(5, '0')}"
487+
obj.instance_variable_set(ivar, 1)
488+
489+
if defined?(RubyVM::Shape)
490+
assert_predicate(RubyVM::Shape.of(obj), :too_complex?)
491+
end
492+
obj.object_id
493+
assert_equal "\x04\bo:\x1CTestMarshal::TooComplex\a:\x19@marshal_too_complexi\x06:\f#{ivar}i\x06".b, Marshal.dump(obj)
494+
end
495+
472496
def test_marshal_complex
473497
assert_raise(ArgumentError){Marshal.load("\x04\bU:\fComplex[\x05")}
474498
assert_raise(ArgumentError){Marshal.load("\x04\bU:\fComplex[\x06i\x00")}

variable.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,9 @@ each_hash_iv(st_data_t id, st_data_t val, st_data_t data)
22552255
{
22562256
struct iv_itr_data * itr_data = (struct iv_itr_data *)data;
22572257
rb_ivar_foreach_callback_func *callback = itr_data->func;
2258+
if (is_internal_id((ID)id)) {
2259+
return ST_CONTINUE;
2260+
}
22582261
return callback((ID)id, (VALUE)val, itr_data->arg);
22592262
}
22602263

0 commit comments

Comments
 (0)