Skip to content

Commit a24570a

Browse files
casperisfinebyroot
andauthored
ObjectSpace.dump: handle Module#set_temporary_name (ruby#12068)
ObjectSpace.dump: handle Module#set_temporary_name [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. Co-authored-by: Jean Boussier <[email protected]>
1 parent f4258aa commit a24570a

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

ext/objspace/objspace_dump.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,8 @@ dump_object(VALUE obj, struct dump_config *dc)
547547
if (dc->cur_obj_klass) {
548548
VALUE mod_name = rb_mod_name(obj);
549549
if (!NIL_P(mod_name)) {
550-
dump_append(dc, ", \"name\":\"");
551-
dump_append(dc, RSTRING_PTR(mod_name));
552-
dump_append(dc, "\"");
550+
dump_append(dc, ", \"name\":");
551+
dump_append_string_value(dc, mod_name);
553552
}
554553
else {
555554
VALUE real_mod_name = rb_mod_name(rb_class_real(obj));

test/objspace/test_objspace.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,12 @@ def test_load_allocation_path_load_from_binary
900900
# load_allocation_path_helper 'iseq = RubyVM::InstructionSequence.load_from_binary(File.binread(path))', to_binary: true
901901
end
902902

903+
def test_escape_class_name
904+
class_name = '" little boby table [Bug #20892]'
905+
json = ObjectSpace.dump(Class.new.tap { |c| c.set_temporary_name(class_name) })
906+
assert_equal class_name, JSON.parse(json)["name"]
907+
end
908+
903909
def test_utf8_method_names
904910
name = "utf8_❨╯°□°❩╯︵┻━┻"
905911
obj = ObjectSpace.trace_object_allocations do

0 commit comments

Comments
 (0)