Skip to content

Commit

Permalink
Properly declare global variables (#941)
Browse files Browse the repository at this point in the history
Fix: ruby/json#672

GC compaction might move these objects around so the global
C variable MUST be declared to the GC so it's updated there too.
  • Loading branch information
byroot authored Nov 1, 2024
1 parent 730455f commit 2b3dc11
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ext/oj/mimic_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,15 @@ void oj_mimic_json_methods(VALUE json) {
} else {
json_error = rb_define_class_under(json, "JSONError", rb_eStandardError);
}

rb_global_variable(&oj_json_parser_error_class);
if (rb_const_defined_at(json, rb_intern("ParserError"))) {
oj_json_parser_error_class = rb_const_get(json, rb_intern("ParserError"));
} else {
oj_json_parser_error_class = rb_define_class_under(json, "ParserError", json_error);
}

rb_global_variable(&oj_json_generator_error_class);
if (rb_const_defined_at(json, rb_intern("GeneratorError"))) {
oj_json_generator_error_class = rb_const_get(json, rb_intern("GeneratorError"));
} else {
Expand All @@ -867,8 +871,8 @@ void oj_mimic_json_methods(VALUE json) {
rb_require("oj/state");
}
// Pull in the JSON::State mimic file.
rb_global_variable(&state_class);
state_class = rb_const_get_at(generator, rb_intern("State"));
rb_gc_register_mark_object(state_class);
}

/* Document-module: JSON
Expand Down
2 changes: 2 additions & 0 deletions ext/oj/rails.c
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,8 @@ static VALUE rails_set_decoder(VALUE self) {
} else {
json_error = rb_define_class_under(json, "JSONError", rb_eStandardError);
}

rb_global_variable(&oj_json_parser_error_class);
if (rb_const_defined_at(json, rb_intern("ParserError"))) {
oj_json_parser_error_class = rb_const_get(json, rb_intern("ParserError"));
} else {
Expand Down

0 comments on commit 2b3dc11

Please sign in to comment.