fix(metricstore): guard stats fast path against empty buffer

Add len(b.data) > 0 guard to prevent infinite loop when an empty linked
buffer is reachable. With the guard false, the normal path handles empty
buffers safely via the existing t < b.start || idx >= len(b.data) check.

Add regression test TestStatsFastPathThenPartialTail to pin the boundary:
three-buffer chain where earlier buffers are fully covered (fast path) and
the last buffer is partially covered (normal path).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Aditya Ujeniya
2026-07-20 10:43:43 +02:00
parent 9eb6d0e83e
commit 9d5268a828
2 changed files with 41 additions and 1 deletions
+1 -1
View File
@@ -74,7 +74,7 @@ func (b *buffer) stats(from, to int64) (Stats, int64, int64, error) {
// past the buffer's real data instead of scanning each point. Any slots
// between len(data) and cap are handled as gaps by the normal loop after
// t advances, so the returned `to` matches the scan semantics.
if idx <= 0 && t <= b.firstWrite() && b.end() <= to {
if len(b.data) > 0 && idx <= 0 && t <= b.firstWrite() && b.end() <= to {
if !b.statsValid {
b.recomputeStats()
}