Skip to content

Reject row/column insert counts past the worksheet and clamp delete counts instead of overflowing (#531) - #627

Merged
h-a-n-a-s-h-i merged 2 commits into
storytold:mainfrom
MarekSpodBiedry:issue-531
Oct 13, 2026
Merged

h-a-n-a-s-h-i merged 2 commits into
storytold:mainfrom
MarekSpodBiedry:issue-531

Conversation

@MarekSpodBiedry

Copy link
Copy Markdown
Contributor

Fixes #531.

home.insertRows, home.insertColumns, home.deleteRows and home.deleteColumns read count through u32_param, which accepts any u32. Nothing bounded at + count before the unchecked additions in insert_lines (the "Format Same As Above" loop at..at + count) and delete_lines (the block end at + count - 1 and the shift origin at + count). With overflow checks on, those panicked and Session::execute returned EngineError::Internal. In release builds they wrapped.

Insert: insert_lines now checks at.checked_add(count) against MAX_ROWS / MAX_COLS before doing anything else and returns BadParams for the command when the new lines would run past the end of the worksheet. This is the same check sheet.moveColumns already uses (checked_add + is_none_or(|end| end > MAX_COLS) → bad("sheet.moveColumns", …)). The existing "cannot shift nonblank cells off of the worksheet" guard is unchanged, and it still catches data being pushed off the sheet.

Delete: delete_lines clamps count to the lines left from at to the end of the worksheet. The block end was already clamped with .min(MAX_ROWS - 1), so the existing intent was already "delete up to the end of the sheet". Clamping count first means the shift origin and Edit::Delete get the same, representable count.

home.insertCells / home.deleteCells with shift: row|column go through the same two functions, so they are covered too.

Tests (crates/engine/tests/line_count_overflow.rs), all on both axes:

  • insert with count: u32::MAX returns BadParams
  • insert after a line with a custom height/width and no cells (the formatting-only state from the issue, where the used-range guard has nothing to check) returns BadParams. Without the fix this hits the overflowing loop.
  • delete with count: u32::MAX doesn't return Internal
  • a normal insert/delete of 3 rows still works

…t shadow it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@h-a-n-a-s-h-i
h-a-n-a-s-h-i merged commit 59872ec into storytold:main Oct 13, 2026
1 check passed
@echelon

echelon commented Oct 13, 2026

Copy link
Copy Markdown
Member

Merged — thank you @MarekSpodBiedry for the contribution!

I pushed a small fix to the branch before merging: the test helper session() was shadowed by a let mut session in the same scope, so the test didn't compile; it is now new_session().

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.

fix(sheet): row and column edits accept counts that overflow their line endpoints

3 participants