Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 4b55804

Browse files
Add overflow checking to Average nullable internal aggregation.
1 parent 0034eef commit 4b55804

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

src/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Inlined/NullableDecimalAverageAggregationOperator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ protected override bool MoveNextCore(ref Pair<decimal, long> currentElement)
118118

119119
if (current.HasValue)
120120
{
121-
sum += current.GetValueOrDefault();
122-
count++;
121+
checked
122+
{
123+
sum += current.GetValueOrDefault();
124+
count++;
125+
}
123126
}
124127
}
125128

src/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Inlined/NullableDoubleAverageAggregationOperator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ protected override bool MoveNextCore(ref Pair<double, long> currentElement)
118118
if ((i++ & CancellationState.POLL_INTERVAL) == 0)
119119
CancellationState.ThrowIfCanceled(_cancellationToken);
120120

121-
sum += current.GetValueOrDefault();
122-
count++;
121+
checked
122+
{
123+
sum += current.GetValueOrDefault();
124+
count++;
125+
}
123126
}
124127
}
125128

src/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Inlined/NullableFloatAverageAggregationOperator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,11 @@ protected override bool MoveNextCore(ref Pair<double, long> currentElement)
119119

120120
if (current.HasValue)
121121
{
122-
sum += current.GetValueOrDefault();
123-
count++;
122+
checked
123+
{
124+
sum += current.GetValueOrDefault();
125+
count++;
126+
}
124127
}
125128
}
126129

src/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Inlined/NullableIntAverageAggregationOperator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,11 @@ protected override bool MoveNextCore(ref Pair<long, long> currentElement)
119119

120120
if (current.HasValue)
121121
{
122-
sum += current.GetValueOrDefault();
123-
count++;
122+
checked
123+
{
124+
sum += current.GetValueOrDefault();
125+
count++;
126+
}
124127
}
125128
}
126129

src/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Inlined/NullableLongAverageAggregationOperator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,11 @@ protected override bool MoveNextCore(ref Pair<long, long> currentElement)
119119
CancellationState.ThrowIfCanceled(_cancellationToken);
120120
if (current.HasValue)
121121
{
122-
sum += current.GetValueOrDefault();
123-
count++;
122+
checked
123+
{
124+
sum += current.GetValueOrDefault();
125+
count++;
126+
}
124127
}
125128
}
126129

src/System.Linq.Parallel/tests/QueryOperators/AverageTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class AverageTests
1414
//
1515
// Average
1616
//
17+
18+
// Get a set of ranges from 0 to each count, with an extra parameter containing the expected average.
1719
public static IEnumerable<object[]> AverageData(object[] counts)
1820
{
1921
Func<int, double> average = x => (x - 1) / 2.0;
@@ -76,6 +78,16 @@ public static void Average_Long_Longrunning(Labeled<ParallelQuery<int>> labeled,
7678
Average_Long(labeled, count, average);
7779
}
7880

81+
[Theory]
82+
[MemberData("Ranges", (object)(new int[] { 2 }), MemberType = typeof(UnorderedSources))]
83+
public static void Average_Long_Overflow(Labeled<ParallelQuery<int>> labeled, int count)
84+
{
85+
Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Select(x => x == 0 ? 1 : long.MaxValue).Average());
86+
Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Select(x => x == 0 ? (long?)1 : long.MaxValue).Average());
87+
Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Average(x => x == 0 ? -1 : long.MinValue));
88+
Functions.AssertThrowsWrapped<OverflowException>(() => labeled.Item.Average(x => x == 0 ? (long?)-1 : long.MinValue));
89+
}
90+
7991
[Theory]
8092
[MemberData("AverageData", (object)(new int[] { 1, 2, 16 }))]
8193
public static void Average_Long_SomeNull(Labeled<ParallelQuery<int>> labeled, int count, double average)

0 commit comments

Comments
 (0)