Skip to content

Commit db6bb02

Browse files
committed
transform neg value when integer overflows
1 parent 8eaaecb commit db6bb02

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

Documentation/Changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9-
### Fixed
9+
### Fixed
10+
-Fix negative coverage exceeding int.MaxValue [#1266](https://github.com/coverlet-coverage/coverlet/issues/1266)
1011
-Fix summary output format for culture de-DE [#1263](https://github.com/coverlet-coverage/coverlet/issues/1263)
1112
-Fix branch coverage issue for finally block with await [#1233](https://github.com/coverlet-coverage/coverlet/issues/1233)
1213
-Fix threshold doesn't work when coverage empty [#1205](https://github.com/coverlet-coverage/coverlet/issues/1205)

src/coverlet.core/Coverage.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,15 @@ private void CalculateCoverage()
420420
var hitLocation = result.HitCandidates[i];
421421
var document = documentsList[hitLocation.docIndex];
422422
int hits = br.ReadInt32();
423+
hits = hits < 0 ? int.MaxValue : hits;
423424

424425
if (hitLocation.isBranch)
425426
{
426427
var branch = document.Branches[new BranchKey(hitLocation.start, hitLocation.end)];
427428
branch.Hits += hits;
429+
430+
if (branch.Hits < 0)
431+
branch.Hits = int.MaxValue;
428432
}
429433
else
430434
{
@@ -437,6 +441,9 @@ private void CalculateCoverage()
437441

438442
var line = document.Lines[j];
439443
line.Hits += hits;
444+
445+
if (line.Hits < 0)
446+
line.Hits = int.MaxValue;
440447
}
441448
}
442449
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
using Coverlet.Core.Samples.Tests;
4+
using Xunit;
5+
6+
namespace Coverlet.Core.Tests
7+
{
8+
public partial class CoverageTests
9+
{
10+
[Fact]
11+
public void Overflow_Issue_1266()
12+
{
13+
string path = Path.GetTempFileName();
14+
try
15+
{
16+
FunctionExecutor.Run(async (string[] pathSerialize) =>
17+
{
18+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<IntegerOverflow>(instance =>
19+
{
20+
instance.Test();
21+
return Task.CompletedTask;
22+
},
23+
persistPrepareResultToFile: pathSerialize[0]);
24+
25+
return 0;
26+
}, new string[] { path });
27+
28+
TestInstrumentationHelper.GetCoverageResult(path)
29+
.Document("Instrumentation.IntegerOverflow.cs")
30+
.AssertLinesCovered(BuildConfiguration.Debug, (16, int.MaxValue), (18, int.MaxValue));
31+
}
32+
finally
33+
{
34+
File.Delete(path);
35+
}
36+
}
37+
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit.Sdk;
7+
8+
namespace Coverlet.Core.Samples.Tests
9+
{
10+
public class IntegerOverflow
11+
{
12+
const long max = (long)int.MaxValue + 1;
13+
14+
public void Test()
15+
{
16+
for (long i = 0; i < max; i++)
17+
{
18+
_ = 1;
19+
}
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)