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

Move rb_global_variable #952

Merged
merged 1 commit into from
Mar 31, 2022
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
2 changes: 1 addition & 1 deletion ext/rbs_extension/unescape.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void rbs_unescape_string(VALUE string) {

if (!HASH) {
HASH = rb_hash_new();
rb_global_variable(&HASH);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GC may be started from inside rb_str_new_literal call where the HASH is not placed on the stack. It means the value of HASH may be freed before the execution of rb_global_variable at the lsat place.

So, moving the rb_global_variable here will ensure it is marked before any possible GC start. 💪

rb_hash_aset(HASH, rb_str_new_literal("\\a"), rb_str_new_literal("\a"));
rb_hash_aset(HASH, rb_str_new_literal("\\b"), rb_str_new_literal("\b"));
rb_hash_aset(HASH, rb_str_new_literal("\\e"), rb_str_new_literal("\e"));
Expand All @@ -29,7 +30,6 @@ void rbs_unescape_string(VALUE string) {
rb_hash_aset(HASH, rb_str_new_literal("\\t"), rb_str_new_literal("\t"));
rb_hash_aset(HASH, rb_str_new_literal("\\v"), rb_str_new_literal("\v"));
rb_hash_aset(HASH, rb_str_new_literal("\\\""), rb_str_new_literal("\""));
rb_global_variable(&HASH);
}

rb_funcall(string, gsub, 2, REGEXP, HASH);
Expand Down