Skip to content

Fix last crash when the count is too large to allocate - #19295

Merged
fdncred merged 1 commit into
nushell:mainfrom
manoj-1407:fix/last-large-size
Oct 19, 2026
Merged

fdncred merged 1 commit into
nushell:mainfrom
manoj-1407:fix/last-large-size

Conversation

@manoj-1407

Copy link
Copy Markdown
Contributor

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

`last` on a byte stream reserved a buffer as large as the requested
count, so a count too large to allocate panicked with a capacity
overflow. The buffer is already trimmed to the count after each read,
so let it grow with the data that is actually read.

Add tests for a huge count on a byte stream and on binary input.

Signed-off-by: G. Manoj Kumar <[email protected]>
@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Oct 19, 2026
@fdncred
fdncred merged commit e03b706 into nushell:main Oct 19, 2026
12 checks passed
@fdncred

fdncred commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.117.0 milestone Oct 19, 2026
@manoj-1407
manoj-1407 deleted the fix/last-large-size branch October 19, 2026 12:46
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