Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent dictionary key being GC while unmarshal a value class #1220

Merged
merged 2 commits into from
Dec 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions ruby/src/IceRuby/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,18 @@ IceRuby::DictionaryInfo::unmarshal(Ice::InputStream* is, const UnmarshalCallback
//
keyType->unmarshal(is, keyCB, Qnil, 0, false);
assert(!NIL_P(keyCB->key));

if (valueType->usesClasses())
{
// Temporarily set the entry with a Qnil value to ensure the key is not GC
// while unmarshaling a value class
if (RB_TYPE_P(keyCB->key, T_STRING))
{
// For string keys create a frozen string to ensure that the key used
// in the map matches the one keep in the closure
keyCB->key = rb_str_new_frozen(keyCB->key);
}
callRuby(rb_hash_aset, hash, keyCB->key, Qnil);
}
//
// The callback will set the dictionary entry with the unmarshaled value,
// so we pass it the key.
Expand Down Expand Up @@ -2744,7 +2755,10 @@ IceRuby::ReadObjectCallback::invoke(const Ice::ObjectPtr& p)
ex.expectedType = _info->id;
throw ex;
}

#ifndef NDEBUG
// With debug builds we force a GC to ensure that all data members are correctly keep alive.
rb_gc();
#endif
_cb->unmarshaled(obj, _target, _closure);
}
else
Expand Down