Skip to content

Commit

Permalink
Fix removing non-existent ivar for too complex
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Nov 1, 2023
1 parent a1e24ab commit 9c6dd25
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 9 additions & 0 deletions test/ruby/test_shapes.rb
Expand Up @@ -265,8 +265,13 @@ def test_run_out_of_shape_for_class
c = Class.new
c.instance_variable_set(:@a, 1)
assert_equal(1, c.instance_variable_get(:@a))
c.remove_instance_variable(:@a)
assert_nil(c.instance_variable_get(:@a))
assert_raise(NameError) do
c.remove_instance_variable(:@a)
end
end;
end

Expand All @@ -290,6 +295,10 @@ class TooComplex < Hash
tc.remove_instance_variable(:@a)
assert_nil(tc.instance_variable_get(:@a))
assert_raise(NameError) do
tc.remove_instance_variable(:@a)
end
end;
end

Expand Down
8 changes: 6 additions & 2 deletions variable.c
Expand Up @@ -2199,7 +2199,9 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name)
case T_MODULE:
IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id);
if (rb_shape_obj_too_complex(obj)) {
st_delete(RCLASS_IV_HASH(obj), (st_data_t *)&id, (st_data_t *)&val);
if (!st_delete(RCLASS_IV_HASH(obj), (st_data_t *)&id, (st_data_t *)&val)) {
val = Qundef;
}
}
else {
rb_shape_transition_shape_remove_ivar(obj, id, shape, &val);
Expand All @@ -2220,7 +2222,9 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name)
if (rb_shape_obj_too_complex(obj)) {
struct gen_ivtbl *ivtbl;
if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
st_delete(ivtbl->as.complex.table, (st_data_t *)&id, (st_data_t *)&val);
if (!st_delete(ivtbl->as.complex.table, (st_data_t *)&id, (st_data_t *)&val)) {
val = Qundef;
}
}
}
else {
Expand Down

0 comments on commit 9c6dd25

Please sign in to comment.