ruby-changes:62601
From: John <ko1@a...>
Date: Tue, 18 Aug 2020 01:48:10 +0900 (JST)
Subject: [ruby-changes:62601] 971857c332 (master): Fix method name escaping in ObjectSpace.dump
https://git.ruby-lang.org/ruby.git/commit/?id=971857c332 From 971857c3326b163c45f02922bb5c5143d54e520d Mon Sep 17 00:00:00 2001 From: John Hawthorn <john@h...> Date: Thu, 9 Jul 2020 14:59:59 -0700 Subject: Fix method name escaping in ObjectSpace.dump It's possible to define methods with any name, even if the parser doesn't support it and it can only be used with ex. send. This fixes an issue where invalid JSON was output from ObjectSpace.dump when a method name needed escaping. diff --git a/ext/objspace/objspace_dump.c b/ext/objspace/objspace_dump.c index d5996f6..f7b9b0b 100644 --- a/ext/objspace/objspace_dump.c +++ b/ext/objspace/objspace_dump.c @@ -313,7 +313,8 @@ dump_object(VALUE obj, struct dump_config *dc) https://github.com/ruby/ruby/blob/trunk/ext/objspace/objspace_dump.c#L313 dump_append(dc, ", \"file\":\"%s\", \"line\":%lu", ainfo->path, ainfo->line); if (RTEST(ainfo->mid)) { VALUE m = rb_sym2str(ainfo->mid); - dump_append(dc, ", \"method\":\"%s\"", RSTRING_PTR(m)); + dump_append(dc, ", \"method\":"); + dump_append_string_value(dc, m); } dump_append(dc, ", \"generation\":%"PRIuSIZE, ainfo->generation); } diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 5073bbe..42fbc3e 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -362,6 +362,24 @@ class TestObjSpace < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/objspace/test_objspace.rb#L362 end end + def test_dump_escapes_method_name + method_name = "foo\"bar" + klass = Class.new do + define_method(method_name) { "TEST STRING" } + end + ObjectSpace.trace_object_allocations_start + + obj = klass.new.send(method_name) + + dump = ObjectSpace.dump(obj) + assert_includes dump, '"method":"foo\"bar"' + + parsed = JSON.parse(dump) + assert_equal "foo\"bar", parsed["method"] + ensure + ObjectSpace.trace_object_allocations_stop + end + def test_dump_reference_addresses_match_dump_all_addresses assert_in_out_err(%w[-robjspace], "#{<<-"begin;"}\n#{<<-'end;'}") do |output, error| begin; -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/