Skip to content

Fix roll panic on empty lists and records - #19285

Merged
fdncred merged 3 commits into
nushell:mainfrom
manoj-1407:fix/roll-empty
Oct 21, 2026
Merged

fdncred merged 3 commits into
nushell:mainfrom
manoj-1407:fix/roll-empty

Conversation

@manoj-1407

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

Copy link
Copy Markdown
Contributor

Description

roll panics when the input is empty. On current main:

[] | roll down
panicked: assertion failed: k <= self.len()

{} | roll left
panicked: assertion failed: mid <= self.len()

[] | roll up --by 3
panicked: attempt to calculate the remainder with a divisor of zero

I found these by running commands against empty and edge-case input. The cause is in vertical_rotate_value and horizontal_rotate_value. Both do n % len, which divides by zero when the list or record is empty. Without --by, the default rotation of 1 on an empty slice also asserts.

The fix uses checked_rem, so an empty list or record gets a rotation of 0 and is returned unchanged. I added tests for roll up, roll down, roll left and roll right on empty input, one for empty records inside a list, and one for a table where only one row is empty.

I ran the roll tests locally and they pass (13). I haven't run the full test suite, so I'm relying on CI for that.

User-facing changes (Release notes)

Fixed a panic when using roll up, roll down, roll left or roll right on an empty list or record.

Additional notes

n/a

`roll up`, `roll down`, `roll left` and `roll right` computed
`n % len` on the input, which divides by zero for an empty list or
an empty record. The default case of `by` also asked the standard
library to rotate an empty slice by one, which asserts.

Return empty input unchanged and add regression tests for each
direction.

Signed-off-by: manoj-1407 <[email protected]>
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

The early returns fix all three panics from the description. The notes below are a simpler way to write the guard and sharper test assertions, none of them blocking.

Findings: 0 High severity · 0 Medium severity · 5 Low severity

Open (5)

Notes from my clanker, checked on 7a4ee4b. Read-only, branch not built; CI is green on all 12 checks. The checked_rem rotation was compared against the current expression with a standalone script (lengths 0-3, --by none/0/1/3/7).

Use `checked_rem` so an empty list or record rotates by zero instead
of special-casing it, which also covers the default rotation.

Compare the rolled value in the empty-input tests, and add a table
test where only one row is empty.

Signed-off-by: G. Manoj Kumar <[email protected]>
@manoj-1407
manoj-1407 requested a review from fdncred October 19, 2026 05:00
@manoj-1407

Copy link
Copy Markdown
Contributor Author

@fdncred thanks for the review! took the checked_rem change, so both early returns and their comments are gone. the roll tests now compare the actual value, and i added the table test with one empty row. left the roll up/down copy for a later pr like you said. roll tests pass locally (13).

@fdncred

fdncred commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

please replay to each review comment to ensure nothing is missed

@manoj-1407

Copy link
Copy Markdown
Contributor Author

replied on each thread, sorry for marking them resolved without replying. i left the roll up / roll down copy alone like you said, happy to look at it in a separate pr if you want. roll tests pass locally.

@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

The rotation now uses checked_rem in both the list and record paths, the early returns and their comments are gone, and the empty list and record tests compare values. The new table test can't fail, and one == ... true assert is left.

Findings: 0 High severity · 0 Medium severity · 2 Low severity

Open (2)

Notes from my clanker, checked on 3f1c185. Read-only, branch not built; CI is green on all 11 checks. Record key order checked on nu 0.116.2: [{a: 1, b: 2} {}] == [{b: 2, a: 1} {}] is true. The roll up / roll down copy stays for a later PR, as agreed.

Record equality ignores key order, so compare the `to nuon` text to
check the key order and that each value moved with its key, and drop
the last `== ... true` assert.

Signed-off-by: G. Manoj Kumar <[email protected]>
@manoj-1407
manoj-1407 requested a review from fdncred October 21, 2026 05:28
@fdncred
fdncred merged commit 64915f0 into nushell:main Oct 21, 2026
11 checks passed
@fdncred

fdncred commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.117.0 milestone Oct 21, 2026
@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Nov 1, 2026
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