Skip to content

Commit 8cf67b8

Browse files
authored
DGS-21279 Ensure unscaled values are positive in decimal conversions (#2490)
* DGS-21279 Ensure unscaled values are positive in decimal conversions * Minor cleanup
1 parent 0736be4 commit 8cf67b8

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/Confluent.SchemaRegistry.Serdes.Protobuf/DecimalExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public static Decimal ToProtobufDecimal(this decimal value)
3535
{
3636
var bytes = GetBytesFromDecimal(value);
3737

38-
var unscaledValueBytes = new byte[12];
39-
Array.Copy(bytes, unscaledValueBytes, unscaledValueBytes.Length);
38+
// Copy the 12 bytes into an array of size 13 so that the last byte is 0,
39+
// which will ensure that the unscaled value is positive.
40+
var unscaledValueBytes = new byte[13];
41+
Array.Copy(bytes, unscaledValueBytes, 12);
4042

4143
var unscaledValue = new BigInteger(unscaledValueBytes);
4244
var scale = bytes[14];

test/Confluent.SchemaRegistry.Serdes.UnitTests/ProtoDecimal.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public void ConvertDecimals()
1919
-123456789123456789.56m,
2020
-1234m,
2121
-1234.5m,
22-
-1234.56m
22+
-1234.56m,
23+
4.1748330066797328106875724500m,
24+
-4.1748330066797328106875724500m
2325
};
2426

2527
foreach (var input in inputs)

0 commit comments

Comments
 (0)