CurtHagenlocher commented on code in PR #546:
URL: https://github.com/apache/arrow-dotnet/pull/546#discussion_r4808858080
##########
src/Apache.Arrow/DecimalUtility.cs:
##########
@@ -192,6 +192,30 @@ private static bool
TryGetDecimalViaBigInteger(ReadOnlySpan<byte> value, int sca
}
}
+ // The most chars formatting or parsing a decimal puts on the stack
+ private const int StackAllocThreshold = 256;
+
+ /// <summary>
+ /// The most decimal digits a value of <paramref name="byteWidth"/>
bytes can have: a byte
+ /// holds fewer than 2.5 digits.
+ /// </summary>
+ private static int MaxDigits(int byteWidth) => byteWidth * 5 / 2 + 1;
+
+ /// <summary>
+ /// The chars needed to hold the digits of <paramref name="value"/>,
padded with zeros to
+ /// <paramref name="scale"/> digits after the point. The precision
isn't used: it comes from
+ /// the type, which can come from untrusted data.
+ /// </summary>
+ private static int ParseBufferLength(string value, int scale, int
neededScale)
+ {
+ long length = (long)value.Length + Math.Max(0, scale -
neededScale);
+ if (length > int.MaxValue)
+ {
+ throw new OverflowException($"Decimal scale {scale} is too
large to parse {value}");
+ }
+ return (int)length;
Review Comment:
Parsing no longer allocates for the scale: padding stops at what the width
can hold, so zero stays zero, a nonzero value throws `OverflowException`
without allocating, and invalid input still throws `ArgumentException`
(`FromStringWithAHugeScale`). Formatting is different: with a huge scale the
plain-notation result itself is that long, so bounding it means changing the
output format or rejecting such types. This PR keeps to the stack use; that can
be considered separately.
##########
src/Apache.Arrow/DecimalUtility.cs:
##########
@@ -209,8 +233,17 @@ internal unsafe static string GetString(in ArrowBuffer
valueBuffer, int index, i
integerValue = -integerValue;
}
+ // The precision and scale come from the type, which can come from
untrusted data, so
+ // size the buffer by the digits the value can have, and keep
large buffers off the stack.
int start = scale + 3;
- Span<char> result = stackalloc char[start + precision];
+ long bufferLength = (long)start + Math.Min(precision,
MaxDigits(byteWidth));
+ if (bufferLength < 0 || bufferLength > int.MaxValue)
+ {
+ throw new OverflowException($"Value: {integerValue} cannot be
formatted with scale {scale}");
+ }
+ Span<char> result = bufferLength <= StackAllocThreshold
+ ? stackalloc char[StackAllocThreshold].Slice(0,
(int)bufferLength)
+ : new char[bufferLength];
Review Comment:
`new char[bufferLength]` with a `long` size is valid C#: array creation
accepts `int`, `uint`, `long` or `ulong` sizes. It builds for every target, and
CI passes.
--
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]