Skip to content

fix: Size decimal string buffers by the value rather than the type's precision - #546

Merged
CurtHagenlocher merged 3 commits into
apache:mainfrom
CurtHagenlocher:decimal-format-precision
Dec 5, 2026
Merged

CurtHagenlocher merged 3 commits into
apache:mainfrom
CurtHagenlocher:decimal-format-precision

Conversation

@CurtHagenlocher

Copy link
Copy Markdown
Contributor

What's changed?

Formatting a decimal as a string (GetString, and so ToString on the decimal arrays) and parsing one from a string sized their buffers by the type's precision and scale. On .NET Core the buffers were on the stack, so a type with a very large precision or scale could exhaust it.

  • The buffers are now sized by the digits the value can have. A value of a given byte width has at most 2.5 digits per byte, plus any zeros the scale adds.
  • Buffers of up to 256 chars stay on the stack; larger ones go on the heap. Sizes are computed so they can't overflow.
  • On .NET Framework the buffers were already on the heap, but were sized by the precision; they are now sized the same way.

The precision is still used to reject values with too many digits, and results are unchanged.

Testing

New tests in DecimalUtilityTests format and parse values with a precision of up to int.MaxValue and a scale of 1,000,000, at byte widths 4, 8, 16 and 32, and format the largest and smallest value of each width with a very large precision. All of Apache.Arrow.Tests passes on net8.0, net462 and net472.

🤖 Generated with Claude Code

https://claude.ai/code/session_012dFdcrBz22tRjiyNwBd7VB

…precision

Formatting a decimal as a string, and parsing one from a string, sized
their buffers by the type's precision and scale, and on .NET Core put
them on the stack. A type with a very large precision or scale could
therefore exhaust the stack.

Size the buffers by the digits the value can have instead: a value of
a given byte width has at most 2.5 digits per byte, plus the zeros the
scale adds. Buffers of up to 256 chars stay on the stack; larger ones
go on the heap. Results are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012dFdcrBz22tRjiyNwBd7VB

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The parsing tests leave the heap-allocation path and buffer-length overflow guard untested, requiring boundary, extreme-scale, and overflow cases across all four byte widths.

1 open issue
What changed in this PR

This PR reduces stack-exhaustion risk when formatting and parsing Arrow decimals with very large precision or scale.

Changes:

  • Sizes buffers from value capacity or input length rather than declared precision.
  • Keeps small buffers on the stack and allocates larger buffers on the heap.
  • Adds regression tests for huge precision, large formatting scales, and byte-width limits.
File Description
test/​Apache.Arrow.Tests/​DecimalUtilityTests.cs Adds extreme-precision and formatting regression tests.
src/​Apache.Arrow/​DecimalUtility.cs Revises buffer sizing, allocation thresholds, and overflow checks.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active critical findings remain: a scale value can trigger an impractically large allocation, and the current buffer allocation uses a long where an int is required.

2 open issues
1 resolved since last review

Review effort: Lite

Comment thread src/Apache.Arrow/DecimalUtility.cs Resolved
Comment thread src/Apache.Arrow/DecimalUtility.cs Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes address untrusted precision and scale risks with bounded, value-based buffer sizing and add regression coverage for extreme decimal cases.

No open issues 🎉

2 resolved since last review

Review effort: Lite

@CurtHagenlocher
CurtHagenlocher merged commit 266791f into apache:main Dec 5, 2026
14 checks passed
@CurtHagenlocher
CurtHagenlocher deleted the decimal-format-precision branch December 5, 2026 14:24
CurtHagenlocher added a commit that referenced this pull request Dec 5, 2026
…precision (#546) (#548)

Brings #546 to maint-23.x. It applied cleanly.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants