summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2024-11-12 19:52:39 +0100
committerJean Boussier <jean.boussier@gmail.com>2024-11-12 20:21:27 +0100
commitee1cd1656fc667840282a4e9c3f2cc5e3154e50e (patch)
tree352eadf78fff128bd42a69336713d58a09463e62
parent6c123649cd9631342395096cf735835820c208b6 (diff)
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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/12067
-rw-r--r--ext/objspace/objspace_dump.c5
-rw-r--r--test/objspace/test_objspace.rb6
2 files changed, 8 insertions, 3 deletions
diff --git a/ext/objspace/objspace_dump.c b/ext/objspace/objspace_dump.c
index bb479b91c5..b1f7ab9804 100644
--- a/ext/objspace/objspace_dump.c
+++ b/ext/objspace/objspace_dump.c
@@ -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));
diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb
index b54a438132..3f08413378 100644
--- a/test/objspace/test_objspace.rb
+++ b/test/objspace/test_objspace.rb
@@ -902,6 +902,12 @@ class TestObjSpace < Test::Unit::TestCase
# 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