Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security and reliability fix for handling of coverage.data #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 11 additions & 5 deletions lib/cover_me/results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class << self
def read_results(path = CoverMe.config.results.store)
data = {}
if File.exists?(path)
data = eval(File.read(path)) || {}
data = File.read(path, {:encoding => 'ASCII-8BIT', :mode => 'r'})
begin
data = Marshal.load(data)
rescue
data = {}
end
end
return data
end
Expand All @@ -31,14 +36,15 @@ def merge_results!(cov_results, path = CoverMe.config.results.store)
end
end

File.open(path, 'w') do |f|
f.write(data.inspect)
File.open(path, {:encoding => 'ASCII-8BIT', :mode => 'w'}) do |f|
data = Marshal.dump(data)
f.write(data)
end

return data
end

end

end # Results
end # CoverMe
end # CoverMe
17 changes: 12 additions & 5 deletions spec/cover_me/results_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
res = {'file1' => [0, nil, 1],
'file2' => [nil, 1, nil]}
File.open(CoverMe.config.results.store, 'w') do |f|
f.write(res.inspect)
f.write(Marshal.dump(res))
end
end

Expand All @@ -31,7 +31,14 @@
res.should be_kind_of(Hash)
res.should be_empty
end


it "should return empty results if reading the file fails with a Syntax error" do
File.stub!(:read).and_return('{garbage}')
res = CoverMe::Results.read_results
res.should be_kind_of(Hash)
res.should be_empty
end

end

describe "merge_results!" do
Expand All @@ -46,16 +53,16 @@
res.should be_kind_of(Hash)
res.should == {'file1' => [0, 1, 2],
'file2' => [nil, 1, 0]}
File.read(CoverMe.config.results.store).should == res.inspect
File.read(CoverMe.config.results.store).should == Marshal.dump(res)
end

it "should merge the results and create a new file if there isn't one" do
res = CoverMe::Results.merge_results!(@more_results, @idontexist_path)
res.should be_kind_of(Hash)
res.should == @more_results
File.read(@idontexist_path).should == res.inspect
File.read(@idontexist_path).should == Marshal.dump(res)
end

end

end