Skip to content

Commit

Permalink
Fix reading numerics with big scale difference (#5851)
Browse files Browse the repository at this point in the history
Fixes #5848

(cherry picked from commit 9ca526b)
  • Loading branch information
vonzshik committed Sep 25, 2024
1 parent ae45245 commit 864b999
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Npgsql/Internal/Converters/Primitive/PgNumeric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,24 @@ internal static decimal ToDecimal(short scale, short weight, ushort sign, Span<s
result += digit;

if (scaleDifference < 0)
result /= UIntPowers10[-scaleDifference];
{
// Doesn't look like we can loop even once, but just to be on a safe side
while (scaleDifference < 0)
{
var scaleChunk = Math.Min(MaxUIntScale, -scaleDifference);
result /= UIntPowers10[scaleChunk];
scaleDifference += scaleChunk;
}
}
else
{
while (scaleDifference > 0)
{
var scaleChunk = Math.Min(MaxUIntScale, scaleDifference);
result *= UIntPowers10[scaleChunk];
scaleFactor *= UIntPowers10[scaleChunk];
scaleDifference -= scaleChunk;
}
}
}

result *= scaleFactor;
Expand Down
2 changes: 2 additions & 0 deletions test/Npgsql.Tests/Types/NumericTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class NumericTests : MultiplexingTestBase

// Bug 2033
new object[] { "0.0036882500000000000000000000", 0.0036882500000000000000000000M },
// Bug 5848
new object[] { "10836968.715000000000000000000000", 10836968.715000000000000000000000M },

new object[] { "936490726837837729197", 936490726837837729197M },
new object[] { "9364907268378377291970000", 9364907268378377291970000M },
Expand Down

0 comments on commit 864b999

Please sign in to comment.