Bug description
If i try to serialize an object that contains a BigInteger-Value, i received an arithmetic-exception. I need BigInteger to calculate large amount of factorials (for some other calculation). When the calculation is ready (from various compute-units), i try to send this via Kafka as BinaryFileMessage.
IMHO the factorial from 1 to 299 are in a specific range of 255 bytes. Above 299 (or the border of 255 bytes), the conversion with MessagePack stucks with the exception following:
Length BigInteger: 256
Unhandled exception. MessagePack.MessagePackSerializationException: Failed to serialize Test value.
---> System.OverflowException: Arithmetic operation resulted in an overflow.
at MessagePack.Formatters.BigIntegerFormatter.Serialize(MessagePackWriter& writer, BigInteger value, MessagePackSerializerOptions options)
at MessagePack.Formatters.TestFormatter1.Serialize(MessagePackWriter& writer, Test value, MessagePackSerializerOptions options)
at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
--- End of inner exception stack trace ---
at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
at MessagePack.MessagePackSerializer.Serialize[T](T value, MessagePackSerializerOptions options, CancellationToken cancellationToken)
at Program.<Main>$(String[] args) in C:\Users\lluec\projekte\LoopSpeedTest\LoopSpeedTest\Program.cs:line 26
In the meantime, I can use Messagepack by not working with BigInteger directly, but by converting the value of the BigInteger into a byte[] and then serialising it.
Repro steps
Here is some code that demonstrates the behavior (simple .net-console app):
If you set end to 299, all works as expected, set it to 300 see above
var start = 1;
var end = 300;
var sw = Stopwatch.StartNew();
BigInteger result = ParallelEnumerable
.Range(start, end)
.Aggregate(
() => BigInteger.One,
(localProduct, i) => localProduct * i,
(totalProduct, localProduct) => totalProduct * localProduct,
finalProduct => finalProduct);
var foo = new Test(end, result);
Console.WriteLine($"Length BigInteger: {result.ToByteArray().Length}");
var bt = MessagePackSerializer.Serialize(foo);
sw.Stop();
Console.WriteLine($"Length: {bt.Length}");
Console.WriteLine($"Time: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("Finished work");
[MessagePackObject(true)]
public record Test(int Id, BigInteger Value);
Expected behavior
MessagePack should work with BigInteger-values (internal data-value size) greater 256 bytes.
Actual behavior
Arithmetic overflow exception
-
Version used:
-- 2.5.187
-- 3.0.238-rc.1
-
Runtime: (e.g. .NET Framework, .NET Core, Unity, mono, etc.)
dotnet --info
.NET SDK:
Version: 8.0.403
Commit: c64aa40a71
Workload version: 8.0.400-manifests.18f19b92
MSBuild version: 17.11.9+a69bbaaf5
Laufzeitumgebung:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Users\lluec\scoop\apps\dotnet-sdk\current\sdk\8.0.403\
Installierte .NET-Workloads:
Konfiguriert für die Verwendung loose manifests beim Installieren neuer Manifeste.
Es sind keine installierten Workloads zum Anzeigen vorhanden.
Host:
Version: 8.0.10
Architecture: x64
Commit: 81cabf2857
.NET SDKs installed:
8.0.403 [C:\Users\lluec\scoop\apps\dotnet-sdk\current\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.10 [C:\Users\lluec\scoop\apps\dotnet-sdk\current\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.10 [C:\Users\lluec\scoop\apps\dotnet-sdk\current\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.10 [C:\Users\lluec\scoop\apps\dotnet-sdk\current\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
None
Environment variables:
DOTNET_ROOT [C:\Users\lluec\scoop\apps\dotnet-sdk\current]
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
Additional context
Add any other context about the problem here.
Bug description
If i try to serialize an object that contains a BigInteger-Value, i received an arithmetic-exception. I need BigInteger to calculate large amount of factorials (for some other calculation). When the calculation is ready (from various compute-units), i try to send this via Kafka as BinaryFileMessage.
IMHO the factorial from 1 to 299 are in a specific range of 255 bytes. Above 299 (or the border of 255 bytes), the conversion with MessagePack stucks with the exception following:
In the meantime, I can use Messagepack by not working with BigInteger directly, but by converting the value of the BigInteger into a byte[] and then serialising it.
Repro steps
Here is some code that demonstrates the behavior (simple .net-console app):
If you set end to 299, all works as expected, set it to 300 see above
var start = 1; var end = 300; var sw = Stopwatch.StartNew(); BigInteger result = ParallelEnumerable .Range(start, end) .Aggregate( () => BigInteger.One, (localProduct, i) => localProduct * i, (totalProduct, localProduct) => totalProduct * localProduct, finalProduct => finalProduct); var foo = new Test(end, result); Console.WriteLine($"Length BigInteger: {result.ToByteArray().Length}"); var bt = MessagePackSerializer.Serialize(foo); sw.Stop(); Console.WriteLine($"Length: {bt.Length}"); Console.WriteLine($"Time: {sw.ElapsedMilliseconds} ms"); Console.WriteLine("Finished work"); [MessagePackObject(true)] public record Test(int Id, BigInteger Value);Expected behavior
MessagePack should work with BigInteger-values (internal data-value size) greater 256 bytes.
Actual behavior
Arithmetic overflow exception
Version used:
-- 2.5.187
-- 3.0.238-rc.1
Runtime: (e.g. .NET Framework, .NET Core, Unity, mono, etc.)
dotnet --info
.NET SDK:
Version: 8.0.403
Commit: c64aa40a71
Workload version: 8.0.400-manifests.18f19b92
MSBuild version: 17.11.9+a69bbaaf5
Laufzeitumgebung:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Users\lluec\scoop\apps\dotnet-sdk\current\sdk\8.0.403\
Installierte .NET-Workloads:
Konfiguriert für die Verwendung loose manifests beim Installieren neuer Manifeste.
Es sind keine installierten Workloads zum Anzeigen vorhanden.
Host:
Version: 8.0.10
Architecture: x64
Commit: 81cabf2857
.NET SDKs installed:
8.0.403 [C:\Users\lluec\scoop\apps\dotnet-sdk\current\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.10 [C:\Users\lluec\scoop\apps\dotnet-sdk\current\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.10 [C:\Users\lluec\scoop\apps\dotnet-sdk\current\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.10 [C:\Users\lluec\scoop\apps\dotnet-sdk\current\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
None
Environment variables:
DOTNET_ROOT [C:\Users\lluec\scoop\apps\dotnet-sdk\current]
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
Additional context
Add any other context about the problem here.