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
Copy Markdown
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
Copy Markdown
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
@anhosh anhosh mentioned this pull request Mar 29, 2026
anhosh added a commit that referenced this pull request Mar 29, 2026
* impl Index<TabIndex> for LeafNode (#311)

* fix(layout): prevent panics on extreme window shrink (#307) (#309)

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

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

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.

* Simplify the clamping

---------

Co-authored-by: Adanos020 <adanos020@gmail.com>

* Refactor index tuples to structs (#312)

* Refactor index tuples to structs

* add convenience functions on NodePath and TabPat

* reorganize imports

* refactor some fallible methods to return an Error

---------

Co-authored-by: Adam Gąsior <adanos020@gmail.com>

* Update egui/eframe to 0.34 (#314)

* Update egui/eframe to 0.34

- Bump egui from 0.33 to 0.34
- Bump eframe from 0.33 to 0.34
- Deprecate DockArea::show(ctx) in favor of show_inside(ui)
- Migrate all examples to eframe's new App::ui(&mut Ui) API
- Replace deprecated ctx.style() with ctx.global_style()
- Fix deprecated warnings in doc-tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: cargo fmt

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Update changelog

* Update Cargo.toml and README.md

* Fix clippy warnings

---------

Co-authored-by: Jonathan Chan Kwan Yin <sofe2038@gmail.com>
Co-authored-by: Matias Lopez <109480516+MatiasLopezING@users.noreply.github.com>
Co-authored-by: Ivan <ixentrum@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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