Skip to content

fix: check small-crate length and offset arithmetic - #12340

Merged
Rich-T-kid merged 2 commits into
apache:mainfrom
Rich-T-kid:audit/arrow-integration-testing-overflow
Nov 20, 2026
Merged

Rich-T-kid merged 2 commits into
apache:mainfrom
Rich-T-kid:audit/arrow-integration-testing-overflow

Conversation

@Rich-T-kid

@Rich-T-kid Rich-T-kid commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Several small support crates trust externally supplied lengths, offsets, or counts before narrowing them or using them for allocation. Oversized inputs can wrap, panic, or request an invalid capacity instead of returning an error.

What changes are included in this PR?

  • arrow-integration-testing converts batch row counts to i64 and uses checked addition for FlightInfo.total_records.
  • arrow-csv checks decoder buffer-size arithmetic before resizing.
  • arrow-integration-test checks narrow JSON integers, offsets, view ranges, fixed-size binary capacity, and Decimal256 width.
  • arrow-pyarrow uses fallible reservation for Python iterable length hints.

This completes the overflow audit for these four small crates.

Are these changes tested?

The existing crate tests cover the affected decoding and conversion paths. The new checks are direct checked arithmetic and checked conversions, so this PR does not add synthetic maximum-size fixtures.

Are there any significant user-facing changes?

Yes. Invalid or oversized integration JSON that previously truncated values or panicked now returns ParseError or JsonError.

@boring-cyborg boring-cyborg Bot added the arrow-core Core crates related to representing arrow data label Nov 19, 2026
@Rich-T-kid
Rich-T-kid marked this pull request as ready for review November 19, 2026 19:38
@Rich-T-kid
Rich-T-kid force-pushed the audit/arrow-integration-testing-overflow branch from 9bd1fa8 to b69f481 Compare November 19, 2026 19:39
@Rich-T-kid
Rich-T-kid force-pushed the audit/arrow-integration-testing-overflow branch from b69f481 to 04061d5 Compare November 19, 2026 19:49
@boring-cyborg boring-cyborg Bot added arrow-csv arrow-csv crate arrow-pyarrow arrow-pyarrow crate labels Nov 19, 2026
@Rich-T-kid Rich-T-kid changed the title fix(arrow-flight): check integration record counts fix: check small-crate length and offset arithmetic Nov 19, 2026
@Rich-T-kid

Copy link
Copy Markdown
Contributor Author

I audited these crates for realistic externally controlled arithmetic rather than replacing arithmetic mechanically.

The changed callsites are worth guarding because their operands cross an external boundary and feed a narrow representation, allocation, or slice:

  • arrow-integration-testing: RecordBatch::num_rows() is usize, while Flight exposes total_records as i64. Both the per-batch conversion and the aggregate can overflow independently.
  • arrow-csv: caller-provided row limits and schema column counts are multiplied before Vec::resize; this is especially reachable on 32-bit targets and previously overflowed before allocation.
  • arrow-integration-test: integration JSON directly supplies narrow integer values, offsets, sizes, and fixed-width lengths. Unchecked casts silently changed the represented value, while offset + size and Decimal256 copies could panic before validation.
  • arrow-pyarrow: Python controls __len__, and that value was passed directly to Vec::with_capacity. A bad length hint could turn malformed external input into a Rust allocation panic.

I left arithmetic alone where it is dominated by an existing slice or buffer length, uses fixed small enum ranges, is a loop counter bounded by an allocated collection, or is an intentional physical/logical integer reinterpretation. Those cases do not have the same overflow path and changing them would make this a mechanical audit rather than a targeted one.

@Rich-T-kid

Rich-T-kid commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

beware this pr is 100% AI generated. I think its a good sign that we can close #12325 relatively soon and reduce the number of PR's that are just replacing arithmetic with overflow checks.

I did review it as well.

cc @Jefffrey @etseidl @mbutrovich

@etseidl

etseidl commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Thanks @Rich-T-kid. Here's my 100% AI generated review 😄 Take with a <fill-in-the-unit> of salt.

P2 — Capacity checks still allow allocation-layout panics

The new checks validate usize arithmetic, but vectors also impose byte-capacity limits.

  • CSV — arrow-csv/src/reader/records.rs:86–90: With one column and

      to_read = isize::MAX as usize / size_of::<usize>() + 1

    the element-count checks succeed, then offsets.resize(...) panics with capacity overflow. The later data-capacity guard is never reached. This is reachable through the public batch-size setting.

  • FixedSizeBinary — arrow-integration-test/src/lib.rs:711–717: With byte width 1 and count = isize::MAX as usize + 1, the multiplication check succeeds, but the builder’s Vec::with_capacity still panics.

I reproduced both without attempting huge allocations. These are pre-existing gaps left uncovered, not new regressions. They belong within the PR’s claimed capacity audit.

Suggested fix: Check vector byte/layout limits before allocation, or use fallible reservation. For CSV, preflight both buffers before resizing either.

Coverage gap — No regression tests for the new error paths

The explanation that tests would require maximum-size fixtures isn’t convincing. Small cases can cover:

  • Int8 128 and UInt8 256.
  • An oversized Decimal256 decimal string.
  • A view with overflowing offset-plus-size and a tiny buffer.
  • The allocation-free capacity cases above.
  • Flight overflow using zero-column batches with explicit row counts.

Existing happy-path tests don’t exercise these guards.

@etseidl

etseidl commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

More seriously, I like the idea of getting out in front of these valid-but-too-many PRs. I tried a similar audit in the parquet crate lately, but a second look seems warranted since that missed several of the recent issues you list in #12325.

Let me know if you want help dividing the work.

@Rich-T-kid

Copy link
Copy Markdown
Contributor Author

Coverage gap — No regression tests for the new error paths

i left this out on purpose. I think we dont need the test for PR's like these, the error path is very clear and the test just restate the code.

Let me know if you want help dividing the work.

yea that would be great, could you handle the parquet crate related ones?

@etseidl

etseidl commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Coverage gap — No regression tests for the new error paths

i left this out on purpose. I think we dont need the test for PR's like these, the error path is very clear and the test just restate the code.

I agree, but AI's tend to be insistent on these things 😅

Let me know if you want help dividing the work.

yea that would be great, could you handle the parquet crate related ones?

Starting a fast bounded search. I'll do a deeper dive if that seems to bear fruit.

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks like an improvement to me -- thanks @Rich-T-kid and @etseidl

I have some crazy idea on how to improve things, but it may be more complicated and not worth it

self.offsets
.resize(self.offsets_len + to_read * self.num_columns, 0);
let offsets_capacity = to_read
.checked_mul(self.num_columns)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we can avoid spreading this copy/pasta around the codebase a bit with a small helper like

trait ArrowCheckedArith {
  fn mul_or_err(&self, v: Self, msg: &'static str) -> Result<..> {
...
}
}

Then callsites like this could be like

 let offsets_capacity = to_read
  .mul_or_err(self.num_columns, "CSV decoder capacity overflow")
  .map(|capacity| self.offsets_len.add_or_err(capacity, "CSV decoder capacity overflow"))?

We could also drop the msg to keep it more concise but less nice error messages

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this could be an option, the main concerns are the total LOC and any possible perf regressions this may cause. As noted in #11973 there can be signifcant regressions introduced when we add this checked logic to hot loops, adding a possible extra layer of indirection for dynamic dispatch may make that worse.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Indeed we would have to measure it -- I think the way this PR structures things though it will add a bunch more code in the related methods (which may be prevent inlining, etc)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a FYI, I've been working on #12099 trying to prevent overruns without impacting performance. The code involved is seriously sensitive to inlining, and at what level it's inlined. For some of these I think we'll need to discuss if the fix is worth it. In my case, I don't think there's a reported panic, just later errors thrown, or at worst wrong data, but this gets to Andrew's point in the Parquet security discussion. If a file is maliciously altered, what is the "correct" output?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In my mind, if the file is maliciously altered or doesn't conform to the spec there is no well defined answer and it is ok to

  1. Return an error
  2. Return some data (unspecified)
  3. Panic

Ideally it wouldn't panic and it would return a nice error message, but those aren't absolute necessities in my mind, especially for wacky corrupted input

@Rich-T-kid
Rich-T-kid added this pull request to the merge queue Nov 20, 2026
Merged via the queue into apache:main with commit 5681d12 Nov 20, 2026
22 checks passed
@Jefffrey Jefffrey added the bug Squash those bugs label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data arrow-csv arrow-csv crate arrow-pyarrow arrow-pyarrow crate bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants