tsdb: Avoid potential overflow in comparisons

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen 2023-10-10 10:05:21 +02:00
parent 26d07ee8d3
commit 02680b42f6
2 changed files with 38 additions and 7 deletions

View file

@ -63,6 +63,15 @@ func (m *maxHeap) push(item Stat) {
} }
func (m *maxHeap) get() []Stat { func (m *maxHeap) get() []Stat {
slices.SortFunc(m.Items, func(a, b Stat) int { return int(b.Count - a.Count) }) slices.SortFunc(m.Items, func(a, b Stat) int {
switch {
case b.Count < a.Count:
return -1
case b.Count > a.Count:
return 1
default:
return 0
}
})
return m.Items return m.Items
} }

View file

@ -184,17 +184,39 @@ type chunkMetaAndChunkDiskMapperRef struct {
} }
func refLessByMinTimeAndMinRef(a, b chunkMetaAndChunkDiskMapperRef) int { func refLessByMinTimeAndMinRef(a, b chunkMetaAndChunkDiskMapperRef) int {
if a.meta.MinTime == b.meta.MinTime { switch {
return int(a.meta.Ref - b.meta.Ref) case a.meta.MinTime < b.meta.MinTime:
return -1
case a.meta.MinTime > b.meta.MinTime:
return 1
}
switch {
case a.meta.Ref < b.meta.Ref:
return -1
case a.meta.Ref > b.meta.Ref:
return 1
default:
return 0
} }
return int(a.meta.MinTime - b.meta.MinTime)
} }
func lessByMinTimeAndMinRef(a, b chunks.Meta) int { func lessByMinTimeAndMinRef(a, b chunks.Meta) int {
if a.MinTime == b.MinTime { switch {
return int(a.Ref - b.Ref) case a.MinTime < b.MinTime:
return -1
case a.MinTime > b.MinTime:
return 1
}
switch {
case a.Ref < b.Ref:
return -1
case a.Ref > b.Ref:
return 1
default:
return 0
} }
return int(a.MinTime - b.MinTime)
} }
func (oh *OOOHeadIndexReader) Postings(ctx context.Context, name string, values ...string) (index.Postings, error) { func (oh *OOOHeadIndexReader) Postings(ctx context.Context, name string, values ...string) (index.Postings, error) {