labels: make sure estimated size is not negative

Deleted labels are remembered, even if they were not in `base` or were
removed from `add`, so `base+add-del` could go negative.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-04-02 11:17:05 +01:00
parent b987afa7ef
commit e917202766

View File

@ -577,7 +577,11 @@ func (b *Builder) Labels() Labels {
return b.base
}
res := make(Labels, 0, len(b.base)-len(b.del)+len(b.add))
expectedSize := len(b.base) + len(b.add) - len(b.del)
if expectedSize < 1 {
expectedSize = 1
}
res := make(Labels, 0, expectedSize)
for _, l := range b.base {
if slices.Contains(b.del, l.Name) || contains(b.add, l.Name) {
continue