Skip to content

fix: clamp tab index in move_tab to prevent out-of-bounds insert#308

Merged
anhosh merged 2 commits intoanhosh:release-0.19from
enomado:fix/move-tab-index-out-of-bounds
Mar 22, 2026
Merged

fix: clamp tab index in move_tab to prevent out-of-bounds insert#308
anhosh merged 2 commits intoanhosh:release-0.19from
enomado:fix/move-tab-index-out-of-bounds

Conversation

@enomado
Copy link
Contributor

@enomado enomado commented Mar 2, 2026

Summary

When reordering a tab within the same node via drag-and-drop, DockState::move_tab calls remove_tab(src_tab) before insert_tab(index, tab). If the source and destination are the same node, this reduces the tab count by one — making the original destination index potentially out of bounds.

Example: A node has 3 tabs [A, B, C]. Dragging tab B (index 1) to position 3 (the end):

  1. remove_tab(1) → node becomes [A, C] (count = 2)
  2. insert_tab(3, B)panic, index 3 > count 2

Fix

Clamp the insertion index to the current tab count after removal. This is safe because inserting at count is equivalent to appending — which is the correct semantic for "move to the end".

Test plan

  • Drag a tab to the last position within its own node — no panic
  • Drag tabs between different nodes — unchanged behavior
  • Drag tabs to split positions — unchanged behavior

When reordering a tab within the same node, `remove_tab` is called
before `insert_tab`. This reduces the tab count by one, so the
original destination index may now be out of bounds.

Clamp the insertion index to the current tab count to prevent panic.
@anhosh anhosh changed the base branch from main to release-0.19 March 22, 2026 21:01
Copy link
Owner

@anhosh anhosh left a comment

Choose a reason for hiding this comment

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

Thanks for spotting and fixing this!

@anhosh anhosh merged commit 29985e6 into anhosh:release-0.19 Mar 22, 2026
4 checks passed
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.

2 participants