1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2026-04-16 21:15:28 -04:00

Prevent overflow exception for big numbers in SizeSuffix and Fluent.Round

This commit is contained in:
Bogdan
2026-03-25 09:27:25 +02:00
committed by Mark McDowall
parent 01fcc160dd
commit be4a564456
4 changed files with 18 additions and 7 deletions

View File

@@ -17,6 +17,8 @@ namespace NzbDrone.Common.Test.ExtensionTests
[TestCase(-1000000, "-976.6 KB")]
[TestCase(-377487360, "-360.0 MB")]
[TestCase(-1255864686, "-1.2 GB")]
[TestCase(long.MinValue, "-8.0 EB")]
[TestCase(long.MaxValue, "8.0 EB")]
public void should_calculate_string_correctly(long bytes, string expected)
{
bytes.SizeSuffix().Should().Be(expected);

View File

@@ -11,16 +11,21 @@ namespace NzbDrone.Common.Extensions
{
const int bytesInKb = 1024;
if (bytes < 0)
{
return "-" + SizeSuffix(-bytes);
}
if (bytes == 0)
{
return "0 B";
}
if (bytes == long.MinValue)
{
return "-" + SizeSuffix(long.MaxValue);
}
if (bytes < 0)
{
return "-" + SizeSuffix(Math.Abs(bytes));
}
var mag = (int)Math.Log(bytes, bytesInKb);
var adjustedSize = bytes / (decimal)Math.Pow(bytesInKb, mag);

View File

@@ -175,7 +175,9 @@ namespace NzbDrone.Core.Test
[TestCase(199, 100, 100)]
[TestCase(1000, 100, 1000)]
[TestCase(0, 100, 0)]
public void round_to_level(long number, int level, int result)
[TestCase(long.MinValue, 1000, -9223372036854775000L)]
[TestCase(long.MaxValue, 1000, 9223372036854775000L)]
public void round_to_level(long number, int level, long result)
{
number.Round(level).Should().Be(result);
}

View File

@@ -22,7 +22,9 @@ namespace NzbDrone.Core
public static long Round(this long number, long level)
{
return Convert.ToInt64(Math.Floor((decimal)number / level) * level);
return number < 0
? Convert.ToInt64(Math.Ceiling((decimal)number / level) * level)
: Convert.ToInt64(Math.Floor((decimal)number / level) * level);
}
public static string ToBestDateString(this DateTime dateTime)