From a24570a62a584478a7dde5dd716b5c88fe86e9e5 Mon Sep 17 00:00:00 2001 From: Jean byroot Boussier Date: Tue, 12 Nov 2024 20:20:36 +0100 Subject: [PATCH] ObjectSpace.dump: handle Module#set_temporary_name (#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 --- ext/objspace/objspace_dump.c | 5 ++--- test/objspace/test_objspace.rb | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ext/objspace/objspace_dump.c b/ext/objspace/objspace_dump.c index 866a49eff4590e..1812ba1d08c601 100644 --- a/ext/objspace/objspace_dump.c +++ b/ext/objspace/objspace_dump.c @@ -547,9 +547,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)); diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 4e09fe1af7e7d7..7a1d4465c7fa66 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -900,6 +900,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