Skip to content

Commit

Permalink
Fix method name escaping in ObjectSpace.dump
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jhawthorn authored and tenderlove committed Aug 17, 2020
1 parent b52a501 commit 971857c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ext/objspace/objspace_dump.c
Expand Up @@ -313,7 +313,8 @@ dump_object(VALUE obj, struct dump_config *dc)
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);
}
Expand Down
18 changes: 18 additions & 0 deletions test/objspace/test_objspace.rb
Expand Up @@ -362,6 +362,24 @@ def dump_my_heap_please
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;
Expand Down

0 comments on commit 971857c

Please sign in to comment.