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
3 changes: 2 additions & 1 deletion Documentation/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed
### Fixed
-Fix negative coverage exceeding int.MaxValue [#1266](https://github.com/coverlet-coverage/coverlet/issues/1266)
-Fix summary output format for culture de-DE [#1263](https://github.com/coverlet-coverage/coverlet/issues/1263)
-Fix branch coverage issue for finally block with await [#1233](https://github.com/coverlet-coverage/coverlet/issues/1233)
-Fix threshold doesn't work when coverage empty [#1205](https://github.com/coverlet-coverage/coverlet/issues/1205)
Expand Down
11 changes: 11 additions & 0 deletions src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,18 @@ private void CalculateCoverage()
var document = documentsList[hitLocation.docIndex];
int hits = br.ReadInt32();

if (hits == 0)
continue;

hits = hits < 0 ? int.MaxValue : hits;

if (hitLocation.isBranch)
{
var branch = document.Branches[new BranchKey(hitLocation.start, hitLocation.end)];
branch.Hits += hits;

if (branch.Hits < 0)
branch.Hits = int.MaxValue;
}
else
{
Expand All @@ -437,6 +445,9 @@ private void CalculateCoverage()

var line = document.Lines[j];
line.Hits += hits;

if (line.Hits < 0)
line.Hits = int.MaxValue;
}
}
}
Expand Down
65 changes: 65 additions & 0 deletions test/coverlet.core.tests/Coverage/CoverageTests.IntegerOverflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.IO;
using Coverlet.Core.Abstractions;
using Coverlet.Core.Instrumentation;
using Moq;
using Xunit;

namespace Coverlet.Core.Tests
{
public partial class CoverageTests
{
[Fact]
public void CoverageResult_NegativeLineCoverage_TranslatedToMaxValueOfInt32()
{
InstrumenterResult instrumenterResult = new InstrumenterResult
{
HitsFilePath = "HitsFilePath",
SourceLink = "SourceLink",
ModulePath = "ModulePath"
};

instrumenterResult.HitCandidates.Add(new HitCandidate(false, 0, 1, 1));

var document = new Document
{
Index = 0,
Path = "Path0"
};

document.Lines.Add(1, new Line
{
Class = "Class0",
Hits = 0,
Method = "Method0",
Number = 1
});

instrumenterResult.Documents.Add("document", document);

CoveragePrepareResult coveragePrepareResult = new CoveragePrepareResult
{
UseSourceLink = true,
Results = new[] {instrumenterResult},
Parameters = new CoverageParameters()
};

Stream memoryStream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(1);
binaryWriter.Write(-1);
memoryStream.Position = 0;

var fileSystemMock = new Mock<IFileSystem>();
fileSystemMock.Setup(x => x.Exists(It.IsAny<string>())).Returns(true);
fileSystemMock.Setup(x => x.NewFileStream(It.IsAny<string>(), FileMode.Open, FileAccess.Read))
.Returns(memoryStream);

var coverage = new Coverage(coveragePrepareResult, new Mock<ILogger>().Object, new Mock<IInstrumentationHelper>().Object,
fileSystemMock.Object, new Mock<ISourceRootTranslator>().Object);

var coverageResult = coverage.GetCoverageResult();
coverageResult.Document("document").AssertLinesCovered(BuildConfiguration.Debug, (1, int.MaxValue));

}
}
}