Skip to content

Commit

Permalink
ObjectSpace.dump: handle Module#set_temporary_name
Browse files Browse the repository at this point in the history
[Bug #20892]

Until the introduction of that method, it was impossible for a
Module name not to be valid JSON, hence it wasn't going through
the slower escaping function.

This assumption no longer hold.
  • Loading branch information
byroot committed Nov 12, 2024
1 parent 6c12364 commit ee1cd16
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 2 additions & 3 deletions ext/objspace/objspace_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,8 @@ dump_object(VALUE obj, struct dump_config *dc)
if (dc->cur_obj_klass) {
VALUE mod_name = rb_mod_name(obj);
if (!NIL_P(mod_name)) {
dump_append(dc, ", \"name\":\"");
dump_append(dc, RSTRING_PTR(mod_name));
dump_append(dc, "\"");
dump_append(dc, ", \"name\":");
dump_append_string_value(dc, mod_name);
}
else {
VALUE real_mod_name = rb_mod_name(rb_class_real(obj));
Expand Down
6 changes: 6 additions & 0 deletions test/objspace/test_objspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,12 @@ def test_load_allocation_path_load_from_binary
# load_allocation_path_helper 'iseq = RubyVM::InstructionSequence.load_from_binary(File.binread(path))', to_binary: true
end

def test_escape_class_name
class_name = '" little boby table [Bug #20892]'
json = ObjectSpace.dump(Class.new.tap { |c| c.set_temporary_name(class_name) })
assert_equal class_name, JSON.parse(json)["name"]
end

def test_utf8_method_names
name = "utf8_❨╯°□°❩╯︵┻━┻"
obj = ObjectSpace.trace_object_allocations do
Expand Down

0 comments on commit ee1cd16

Please sign in to comment.