diff options
author | Jb Evain <jbevain@gmail.com> | 2009-01-22 18:40:38 +0300 |
---|---|---|
committer | Jb Evain <jbevain@gmail.com> | 2009-01-22 18:40:38 +0300 |
commit | 99a81c984e4714c162e00f650b582fe1ca7e0bf4 (patch) | |
tree | 5e4b320afec4095836a93c3e4fd9859df33ead6f /mcs/class/System.Core/System.Linq/Enumerable.cs | |
parent | 7190a31e60832a86e8ef49e4790252125b019fd5 (diff) |
2009-01-22 Jb Evain <jbevain@novell.com>
* Enumerable.cs (Sum): check the sum operations for possible overfows.
svn path=/trunk/mcs/; revision=124216
Diffstat (limited to 'mcs/class/System.Core/System.Linq/Enumerable.cs')
-rw-r--r-- | mcs/class/System.Core/System.Linq/Enumerable.cs | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/mcs/class/System.Core/System.Linq/Enumerable.cs b/mcs/class/System.Core/System.Linq/Enumerable.cs index 6cc8c0a25fe..820790a0f4d 100644 --- a/mcs/class/System.Core/System.Linq/Enumerable.cs +++ b/mcs/class/System.Core/System.Linq/Enumerable.cs @@ -1855,21 +1855,21 @@ namespace System.Linq { Check.Source (source); - return Sum<int, int> (source, (a, b) => a + b); + return Sum<int, int> (source, (a, b) => checked (a + b)); } public static int? Sum (this IEnumerable<int?> source) { Check.Source (source); - return source.SumNullable<int?, int?> (0, (a, b) => a.HasValue ? a + b : a); + return source.SumNullable<int?, int?> (0, (total, element) => element.HasValue ? checked (total + element) : total); } public static int Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector) { Check.SourceAndSelector (source, selector); - return Sum<TSource, int> (source, (a, b) => a + selector (b)); + return Sum<TSource, int> (source, (a, b) => checked (a + selector (b))); } public static int? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector) @@ -1878,7 +1878,7 @@ namespace System.Linq return source.SumNullable<TSource, int?> (0, (a, b) => { var value = selector (b); - return value.HasValue ? a + value.Value : a; + return value.HasValue ? checked (a + value.Value) : a; }); } @@ -1886,21 +1886,21 @@ namespace System.Linq { Check.Source (source); - return Sum<long, long> (source, (a, b) => a + b); + return Sum<long, long> (source, (a, b) => checked (a + b)); } public static long? Sum (this IEnumerable<long?> source) { Check.Source (source); - return source.SumNullable<long?, long?> (0, (a, b) => a.HasValue ? a + b : a); + return source.SumNullable<long?, long?> (0, (total, element) => element.HasValue ? checked (total + element) : total); } public static long Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector) { Check.SourceAndSelector (source, selector); - return Sum<TSource, long> (source, (a, b) => a + selector (b)); + return Sum<TSource, long> (source, (a, b) => checked (a + selector (b))); } public static long? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector) @@ -1909,7 +1909,7 @@ namespace System.Linq return source.SumNullable<TSource, long?> (0, (a, b) => { var value = selector (b); - return value.HasValue ? a + value.Value : a; + return value.HasValue ? checked (a + value.Value) : a; }); } @@ -1917,21 +1917,21 @@ namespace System.Linq { Check.Source (source); - return Sum<double, double> (source, (a, b) => a + b); + return Sum<double, double> (source, (a, b) => checked (a + b)); } public static double? Sum (this IEnumerable<double?> source) { Check.Source (source); - return source.SumNullable<double?, double?> (0, (a, b) => a.HasValue ? a + b : a); + return source.SumNullable<double?, double?> (0, (total, element) => element.HasValue ? checked (total + element) : total); } public static double Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector) { Check.SourceAndSelector (source, selector); - return Sum<TSource, double> (source, (a, b) => a + selector (b)); + return Sum<TSource, double> (source, (a, b) => checked (a + selector (b))); } public static double? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector) @@ -1940,7 +1940,7 @@ namespace System.Linq return source.SumNullable<TSource, double?> (0, (a, b) => { var value = selector (b); - return value.HasValue ? a + value.Value : a; + return value.HasValue ? checked (a + value.Value) : a; }); } @@ -1948,21 +1948,21 @@ namespace System.Linq { Check.Source (source); - return Sum<float, float> (source, (a, b) => a + b); + return Sum<float, float> (source, (a, b) => checked (a + b)); } public static float? Sum (this IEnumerable<float?> source) { Check.Source (source); - return source.SumNullable<float?, float?> (0, (a, b) => a.HasValue ? a + b : a); + return source.SumNullable<float?, float?> (0, (total, element) => element.HasValue ? checked (total + element) : total); } public static float Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector) { Check.SourceAndSelector (source, selector); - return Sum<TSource, float> (source, (a, b) => a + selector (b)); + return Sum<TSource, float> (source, (a, b) => checked (a + selector (b))); } public static float? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector) @@ -1971,7 +1971,7 @@ namespace System.Linq return source.SumNullable<TSource, float?> (0, (a, b) => { var value = selector (b); - return value.HasValue ? a + value.Value : a; + return value.HasValue ? checked (a + value.Value) : a; }); } @@ -1979,21 +1979,21 @@ namespace System.Linq { Check.Source (source); - return Sum<decimal, decimal> (source, (a, b) => a + b); + return Sum<decimal, decimal> (source, (a, b) => checked (a + b)); } public static decimal? Sum (this IEnumerable<decimal?> source) { Check.Source (source); - return source.SumNullable<decimal?, decimal?> (0, (a, b) => a.HasValue ? a + b : a); + return source.SumNullable<decimal?, decimal?> (0, (total, element) => element.HasValue ? checked (total + element) : total); } public static decimal Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector) { Check.SourceAndSelector (source, selector); - return Sum<TSource, decimal> (source, (a, b) => a + selector (b)); + return Sum<TSource, decimal> (source, (a, b) => checked (a + selector (b))); } public static decimal? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector) @@ -2002,7 +2002,7 @@ namespace System.Linq return source.SumNullable<TSource, decimal?> (0, (a, b) => { var value = selector (b); - return value.HasValue ? a + value.Value : a; + return value.HasValue ? checked (a + value.Value) : a; }); } |