Skip to content

Fix chunks crash when the chunk size is too large to allocate - #19286

Merged
fdncred merged 2 commits into
nushell:mainfrom
manoj-1407:fix/chunks-large-size
Oct 18, 2026
Merged

fdncred merged 2 commits into
nushell:mainfrom
manoj-1407:fix/chunks-large-size

Conversation

@manoj-1407

@manoj-1407 manoj-1407 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

chunks crashes when the chunk size is too large to allocate. On current main:

[1 2 3] | chunks 9223372036854775807
panicked: capacity overflow

0x[11 22 33] | chunks 9223372036854775807
memory allocation of 9223372036854775807 bytes failed

The stream versions ([0 1 2] | every 1 | chunks 9223372036854775807 and random binary 16b | chunks 9223372036854775807) crash the same way, as pointed out in review.

All of them come from Vec::with_capacity(self.size), where size is the number the user typed. The whole chunk size is reserved before any input is read.

For lists, each chunk is now built with once(first).chain(..).collect(). That has an exact size hint for list input, so it allocates once, and nothing is reserved from the user's number. For binary input, ChunkRead starts with an empty buffer and grows it with the bytes it reads.

Trade-off: stream and binary input now grow by doubling instead of one up-front allocation, so there is a bit more copying. Memory use follows the data, not the argument.

I added tests for a huge chunk size on a list, a list stream, a binary and a byte stream. I ran the chunks tests locally and they pass. I did not run the full workspace suite.

window has the same pattern and I'm sending it as a separate PR.

AI assistance: I used Claude to help find the issue and draft the change. I reviewed and tested it myself.

User-facing changes (Release notes)

Fixed a crash when chunks is given a chunk size too large to allocate, such as 9223372036854775807.

Additional notes

n/a

`chunks` reserved a buffer as large as the requested chunk size
before reading any input. A size larger than the memory available
panicked with a capacity overflow for lists and aborted with an
allocation failure for binary input.

Let the buffers grow with the data that is actually read instead,
and add tests for a chunk size larger than the input.

Signed-off-by: G. Manoj Kumar <[email protected]>

@fdncred fdncred 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.

Review overview

🟡 Changes recommended

Both allocation changes are correct, but the new tests skip the list stream and byte stream inputs, which crash on main too.

Findings: 0 High severity · 1 Medium severity · 4 Low severity

Open (5)

Notes from my clanker, checked on 193370f. I didn't build the branch. The crash repros ran on a main release build (973c590), the suggested test expressions were checked there too, and the allocation counts come from a standalone Rust program.

Comment thread crates/nu-command/src/filters/chunks.rs Resolved
Add tests for huge chunk sizes on list streams and byte streams, and
compare the chunk contents in the binary test instead of only the
count.

Build each list chunk with `once(first).chain(..).collect()` so it
allocates once instead of twice.

Signed-off-by: G. Manoj Kumar <[email protected]>
@manoj-1407 manoj-1407 changed the title Fix chunks crash when the chunk size is larger than the input Fix chunks crash when the chunk size is too large to allocate Oct 18, 2026
@manoj-1407
manoj-1407 requested a review from fdncred October 18, 2026 13:47
@manoj-1407

Copy link
Copy Markdown
Contributor Author

@fdncred good to go? thanks for the review btw :) pushed a follow-up commit with the stream tests, the value comparison, and the once/chain/collect change, and updated the title and release note. i'll send window as a separate PR

@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Oct 18, 2026
@fdncred
fdncred merged commit a714117 into nushell:main Oct 18, 2026
11 checks passed
@fdncred

fdncred commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Thanks!

@github-actions github-actions Bot added this to the v0.117.0 milestone Oct 18, 2026
@manoj-1407
manoj-1407 deleted the fix/chunks-large-size branch October 18, 2026 18:19
fdncred pushed a commit that referenced this pull request Oct 19, 2026
## Description
`last` crashes on byte streams when the count is too large to allocate.
On current main:

> random binary 16b | last 9223372036854775807 | length
panicked: capacity overflow

It comes from `VecDeque::with_capacity(rows + TAKE as usize)`, where
`rows` is the number the user typed. The loop already drains the buffer
down to `rows` after every read, so the buffer only needs to grow with
the data that is actually read.

The buffer now starts empty, so memory use follows the data and not the
argument. This is the same problem as #19286 (`chunks`) and #19288
(`window`).

I added tests for a huge count on a byte stream and on binary input,
comparing the values. I ran the `last` tests locally and they pass. I
did not run the full workspace suite.

AI assistance: I used Claude to help find the issue and draft the
change. I reviewed and tested it myself.

## User-facing changes (Release notes)
Fixed a crash when `last` is given a count too large to allocate, such
as 9223372036854775807, on byte stream input.

## Additional notes
n/a

Signed-off-by: G. Manoj Kumar <[email protected]>
fdncred pushed a commit that referenced this pull request Oct 20, 2026
)

## Description
`window` crashes when the window size is too large to allocate. On
current main:

> [1 2 3] | window 9223372036854775807
panicked: capacity overflow

> [1 2 3] | window 9223372036854775807 --stride 9223372036854775807
panicked: capacity overflow

Both come from `Vec::with_capacity` taking the size the user typed.
`WindowOverlapIter` reserved it in `new`, and `WindowGapIter` reserved
it on every call to `next`.

The overlapping iterator also used `self.window.capacity()` as the
window size, so it depended on the reservation. It now keeps `size` as a
field. The overlap buffer starts empty and is filled with `extend`, and
the gap iterator collects its window, so neither reserves the typed
size.

This is the same problem that #19286 fixed for `chunks`. With
`--remainder` and a stride equal to the size, `window` hands off to
`chunks`, so that case works now too.

I added tests for a huge size on a list, with a huge stride, on list
streams (both iterators), and with `--remainder`, where they check the
actual windows. I ran the `window` and `chunks` tests locally and they
pass. I did not run the full workspace suite.

AI assistance: I used Claude to help find the issue and draft the
change. I reviewed and tested it myself.

## User-facing changes (Release notes)
Fixed a crash when `window` is given a window size too large to
allocate, such as 9223372036854775807.

## Additional notes
n/a

---------

Signed-off-by: G. Manoj Kumar <[email protected]>
fdncred pushed a commit that referenced this pull request Oct 20, 2026
…locate (#19296)

## Description
`table --abbreviated` crashes when the count is too large to allocate.
On current main:

> 1..5 | table --abbreviated 9223372036854775807
panicked: capacity overflow

> [[a]; [1]] | table --abbreviated 9223372036854775807
panicked: capacity overflow

`stream_collect_abbreviated` reserves the head and the tail with
`with_capacity(size)`, where `size` is the number the user typed. Both
buffers only ever hold rows that are actually read, so they are now
sized from the iterator's size hint: `head` reserves `hint.min(2 * size
+ 1)`, which is its final length with the `...` row, and `tail` reserves
`hint.saturating_sub(size).min(size)`. Inputs without a hint, like a
range or `every`, start empty and grow.

The output doesn't change: a count larger than the input shows every
row, as before. This is the same problem as #19286 (`chunks`), #19288
(`window`) and #19295 (`last`).

I added tests that a huge count prints the same table as a normal count,
for a plain list and for a list stream, and a huge-count range case in
`table_abbreviation_cut`. I ran the `table::abbreviation` tests locally
and they pass. I did not run the full workspace suite.

AI assistance: I used Claude to help find the issue and draft the
change. I reviewed and tested it myself.

## User-facing changes (Release notes)
Fixed a crash when `table --abbreviated` is given a row count too large
to allocate, such as 9223372036854775807.

## Additional notes
n/a

---------

Signed-off-by: G. Manoj Kumar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants