Skip to content

Commit d5bc5fe

Browse files
committed
fix FromLabelAdaptersToLabels
Signed-off-by: Coleen Iona Quadros <coleen.quadros27@gmail.com>
1 parent fa9b29e commit d5bc5fe

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ It is recommend to upgrade the storage components first (Receive, Store, etc.) a
1515
### Fixed
1616

1717
- [#8128](https://github.com/thanos-io/thanos/issues/8128): Query-Frontend: Fix panic in `AnalyzesMerge` caused by indexing the wrong slice variable, leading to an out-of-range access when merging more than two query analyses.
18-
- [#8709](https://github.com/thanos-io/thanos/pull/8709): Query: fix panic in `ZLabelsToPromLabels` caused by unsafe pointer cast incompatible with Prometheus `stringlabels` encoding.
18+
- [#8709](https://github.com/thanos-io/thanos/pull/8709): Query/Query-Frontend: fix panic in `ZLabelsToPromLabels` and `FromLabelAdaptersToLabels` caused by unsafe pointer casts incompatible with Prometheus `stringlabels` encoding.
1919

2020
### Added
2121

internal/cortex/cortexpb/compat.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,29 @@ import (
1919
"github.com/thanos-io/thanos/internal/cortex/util"
2020
)
2121

22-
// FromLabelAdaptersToLabels casts []LabelAdapter to labels.Labels.
23-
// It uses unsafe, but as LabelAdapter == labels.Label this should be safe.
24-
// This allows us to use labels.Labels directly in protos.
25-
//
26-
// Note: while resulting labels.Labels is supposedly sorted, this function
27-
// doesn't enforce that. If input is not sorted, output will be wrong.
22+
// FromLabelAdaptersToLabels converts []LabelAdapter to labels.Labels.
2823
func FromLabelAdaptersToLabels(ls []LabelAdapter) labels.Labels {
29-
return *(*labels.Labels)(unsafe.Pointer(&ls))
24+
if len(ls) == 0 {
25+
return labels.EmptyLabels()
26+
}
27+
b := labels.NewScratchBuilder(len(ls))
28+
for _, l := range ls {
29+
b.Add(l.Name, l.Value)
30+
}
31+
b.Sort()
32+
return b.Labels()
3033
}
3134

32-
// FromLabelsToLabelAdapters casts labels.Labels to []LabelAdapter.
33-
// It uses unsafe, but as LabelAdapter == labels.Label this should be safe.
34-
// This allows us to use labels.Labels directly in protos.
35+
// FromLabelsToLabelAdapters converts labels.Labels to []LabelAdapter.
3536
func FromLabelsToLabelAdapters(ls labels.Labels) []LabelAdapter {
36-
return *(*[]LabelAdapter)(unsafe.Pointer(&ls))
37+
if ls.IsEmpty() {
38+
return nil
39+
}
40+
result := make([]LabelAdapter, 0, ls.Len())
41+
ls.Range(func(l labels.Label) {
42+
result = append(result, LabelAdapter{Name: l.Name, Value: l.Value})
43+
})
44+
return result
3745
}
3846

3947
// FromLabelAdaptersToMetric converts []LabelAdapter to a model.Metric.

0 commit comments

Comments
 (0)