Skip to content

bucket: add count guard for branch pages in Stats#1171

Merged
ahrtr merged 1 commit intoetcd-io:mainfrom
quocvibui:fix-stats-branch-page-oob
Mar 31, 2026
Merged

bucket: add count guard for branch pages in Stats#1171
ahrtr merged 1 commit intoetcd-io:mainfrom
quocvibui:fix-stats-branch-page-oob

Conversation

@quocvibui
Copy link
Copy Markdown
Contributor

Bucket.Stats() panics with an index-out-of-range error when iterating branch pages with a zero element count. The leaf page path has a p.Count() != 0 guard (line 628), but the branch page path accesses p.BranchPageElement(p.Count() - 1) without one — causing an underflow when Count() is 0.

This wraps the branch element access in the same guard pattern used for leaf pages.

Fixes #1170

Bucket.Stats() panics with index-out-of-range when iterating branch
pages with zero elements. The leaf page path has a Count() != 0 guard,
but the branch page path accesses BranchPageElement(Count() - 1)
without one. Wrap the branch element access in the same guard.

Fixes etcd-io#1170

Signed-off-by: Quoc Bui <buiviquoc@gmail.com>
@ahrtr
Copy link
Copy Markdown
Member

ahrtr commented Mar 30, 2026

@ahrtr
Copy link
Copy Markdown
Member

ahrtr commented Mar 30, 2026

Please also add a changelog item under https://github.com/etcd-io/bbolt/blob/main/CHANGELOG/CHANGELOG-1.5.md#boltdb, thx

Correct, we need to only add changelog items for 1.4 and. 1.3 instead. We need to backport the PR to 1.4 and 1.3.

Please feel free to backport the PR to 1.4 and 1.3 first, and update chanagelogs in a separate PR later. thx

@ahrtr
Copy link
Copy Markdown
Member

ahrtr commented Mar 30, 2026

Sorry for the confusion. Please do not update changelog in this PR. Please follow my previous comment #1171 (comment)

@quocvibui
Copy link
Copy Markdown
Contributor Author

Done! Backported to 1.4 (#1172) and 1.3 (#1173), and added changelog entries in #1174. Let me know if anything needs adjusting.

@quocvibui quocvibui force-pushed the fix-stats-branch-page-oob branch from fad9692 to 386d5b6 Compare March 30, 2026 14:17
@ahrtr
Copy link
Copy Markdown
Member

ahrtr commented Mar 30, 2026

/ok-to-test

Comment thread bucket.go
// Add header and all element headers.
used := common.PageHeaderSize + (common.BranchPageElementSize * uintptr(p.Count()-1))
used := common.PageHeaderSize
if p.Count() != 0 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note: normally a branch page should always have at least 1 item, otherwise the page will be removed. But the change has no any harm.

@k8s-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ahrtr, quocvibui

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ahrtr ahrtr merged commit cae11e9 into etcd-io:main Mar 31, 2026
87 checks passed
stalltrix pushed a commit to stalltrix/bolt-server that referenced this pull request Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Bucket.Stats panics on branch pages with zero elements (missing count guard)

3 participants