Copilot commented on code in PR #546:
URL: https://github.com/apache/arrow-dotnet/pull/546#discussion_r4808825135
##########
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:
This guard only prevents an integer overflow; it still permits essentially
unbounded allocations. For example, parsing `"0"` with `scale = int.MaxValue -
1` produces `length == int.MaxValue`, so the following allocation attempts a
multi-gigabyte `char[]` for a value that could be represented as zero without
padding. Because scale is type metadata that may be untrusted, reject
impractical buffer sizes (or avoid materializing padding when the value cannot
need it) before allocating to prevent an OOM-based denial of service.
##########
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:
`bufferLength` is a `long`, but array dimensions in this target set require
an `int`; this `new char[bufferLength]` does not compile. The preceding range
check already proves the value fits, so cast it to `int` here.
--
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]