Fix size_t underflow in TabsBase::remove bounds check - #385
Merged
Conversation
When m_tabs is empty, m_tabs.size() - 1 underflows to SIZE_MAX, so the bounds check never triggers and remove() falls through to erase() on an empty vector, causing a crash. Use index >= m_tabs.size() instead, matching the pattern already used by BoxLayout::remove, ListBox::removeItemByIndex and TabContainer::removeTab elsewhere in the codebase.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
`TabsBase::remove(std::size_t index)` checks bounds like this:
```cpp
if (index > m_tabs.size() - 1)
return false;
```
When `m_tabs` is empty, `m_tabs.size() - 1` underflows (unsigned) to `SIZE_MAX`, so the check never returns `false` and execution falls through to:
```cpp
m_tabs.erase(m_tabs.cbegin() + static_caststd::ptrdiff_t(index));
```
which erases from an empty vector at an out-of-range position, invoking undefined behavior (crashes both in a plain optimized build and under `_GLIBCXX_DEBUG`).
Both `Tabs` and `VerticalTabs` inherit `remove()` from `TabsBase` without overriding it, and the header doc comment for this function promises "false is returned when the index was too high" — a promise this code breaks precisely in the empty-container case.
Fix
Rewrite the bounds check as `index >= m_tabs.size()`, which is safe for the empty case and matches the equivalent checks already used elsewhere in the codebase (e.g. `BoxLayout::remove`, `ListBox::removeItemByIndex`, `TabContainer::removeTab`).
Testing
Extracted the exact erase/bounds logic into a standalone reproduction: calling `remove(0)` on an empty container segfaults with the old `> size() - 1` check (both under `-O2` and under `_GLIBCXX_DEBUG`), and correctly returns `false` with no crash after applying this fix.