Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/ruby/coverage/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ static void Ruby_Coverage_Tracer_on_line(rb_event_flag_t event, VALUE data, VALU

// Counts are 1-indexed: index 0 is unused (nil), index N is the hit count
// for source line N. Grow the array if necessary.
while (RARRAY_LEN(tracer->last_counts) <= line) {
rb_ary_push(tracer->last_counts, Qnil);
if (RARRAY_LEN(tracer->last_counts) <= line) {
rb_ary_resize(tracer->last_counts, line + 1);
}

VALUE current = rb_ary_entry(tracer->last_counts, line);
Expand Down
17 changes: 17 additions & 0 deletions test/ruby/coverage/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ def around
expect(counts[1]).to be == 2
end

it "resizes count arrays for sparse high line numbers" do
files = {}
tracer = Ruby::Coverage::Tracer.new{|path, iseq| files[path] ||= []}

tracer.start

path = "/tmp/ruby_coverage_tracer_sparse.rb"
line = 100
Module.new.module_eval("x = 1", path, line)

tracer.stop

counts = files[path]
expect(counts).not.to be_nil
expect(counts[line]).to be == 1
end

it "returns nil from the callback to skip tracking a file" do
files = {}
tracer = Ruby::Coverage::Tracer.new{|path, iseq| nil}
Expand Down
Loading