CurtHagenlocher commented on code in PR #546:
URL: https://github.com/apache/arrow-dotnet/pull/546#discussion_r4808778131
##########
test/Apache.Arrow.Tests/DecimalUtilityTests.cs:
##########
@@ -188,6 +189,68 @@ private void TestFromString(string s, int precision, int
scale, int byteWidth, s
Assert.Equal(result, DecimalUtility.GetString(new
ArrowBuffer(bytes), 0, precision, scale, byteWidth));
}
+ // The precision and scale come from the type, which may come from
untrusted data, so
+ // they must not decide how much stack is used.
+ [Theory]
+ [InlineData(100_000_000)]
+ [InlineData(int.MaxValue)]
+ public void ToStringWithAHugePrecision(int precision)
+ {
+ foreach (int byteWidth in new[] { 4, 8, 16, 32 })
+ {
+ var bytes = new byte[byteWidth];
+ DecimalUtility.GetBytes(-100.12m, 9, 2, byteWidth, bytes);
+ Assert.Equal("-100.12", DecimalUtility.GetString(new
ArrowBuffer(bytes), 0, precision, 2, byteWidth));
+ }
+ }
+
+ [Theory]
+ [InlineData(4)]
+ [InlineData(8)]
+ [InlineData(16)]
+ [InlineData(32)]
+ public void ToStringOfTheLargestValuesWithAHugePrecision(int
byteWidth)
+ {
+ var largest = new byte[byteWidth];
+ largest.AsSpan().Fill(0xFF);
+ largest[byteWidth - 1] = 0x7F;
+ var smallest = new byte[byteWidth];
+ smallest[byteWidth - 1] = 0x80;
+
+ foreach (byte[] bytes in new[] { largest, smallest })
+ {
+ string digits = new BigInteger(bytes).ToString();
+ string expected = digits.Substring(0, digits.Length - 1) +
"." + digits.Substring(digits.Length - 1);
+ Assert.Equal(expected, DecimalUtility.GetString(new
ArrowBuffer(bytes), 0, int.MaxValue, 1, byteWidth));
+ }
+ }
+
+ [Fact]
+ public void ToStringWithAHugeScale()
+ {
+ const int scale = 1_000_000;
+ foreach (int byteWidth in new[] { 4, 8, 16, 32 })
+ {
+ var bytes = new byte[byteWidth];
+ bytes[0] = 1;
+ string expected = "0." + new string('0', scale - 1) + "1";
+ Assert.Equal(expected, DecimalUtility.GetString(new
ArrowBuffer(bytes), 0, 10, scale, byteWidth));
+ }
+ }
+
+ [Theory]
+ [InlineData(100_000_000)]
+ [InlineData(int.MaxValue - 10)]
+ public void FromStringWithAHugePrecision(int precision)
+ {
+ foreach (int byteWidth in new[] { 4, 8, 16, 32 })
+ {
+ var bytes = new byte[byteWidth];
+ DecimalUtility.GetBytes("-100.12", precision, 3,
byteWidth, bytes);
+ Assert.Equal("-100.120", DecimalUtility.GetString(new
ArrowBuffer(bytes), 0, 10, 3, byteWidth));
+ }
+ }
Review Comment:
Thanks — added `FromStringPaddedToALargeScale` and
`FromStringWithALongFraction`, which cover both sides of the 256-char boundary
and a scale of 1,000,000 at all four byte widths. Also added
`FromStringPaddedBeyondTheWidth_Throws` (padding a nonzero value past what the
width can hold) and `FromStringWithAScaleTooLargeToPad_Throws` (`"0"` with
scale `int.MaxValue`).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]