From 50a6efd5ecd91b69ace62af67c46c532e2346ebc Mon Sep 17 00:00:00 2001 From: machine424 Date: Fri, 23 May 2025 11:03:48 +0200 Subject: [PATCH] fix(model/textparse): Labels(): copy the input to avoid dangling references Signed-off-by: machine424 --- model/textparse/openmetricsparse.go | 4 +++- model/textparse/promparse.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/model/textparse/openmetricsparse.go b/model/textparse/openmetricsparse.go index b15fef6d1e..d9c37a78b7 100644 --- a/model/textparse/openmetricsparse.go +++ b/model/textparse/openmetricsparse.go @@ -213,7 +213,9 @@ func (p *OpenMetricsParser) Comment() []byte { // Labels writes the labels of the current sample into the passed labels. func (p *OpenMetricsParser) Labels(l *labels.Labels) { - s := yoloString(p.series) + // Defensive copy in case the following keeps a reference. + // See https://github.com/prometheus/prometheus/issues/16490 + s := string(p.series) p.builder.Reset() metricName := unreplace(s[p.offsets[0]-p.start : p.offsets[1]-p.start]) diff --git a/model/textparse/promparse.go b/model/textparse/promparse.go index 45ce9d87e4..5ca61d1972 100644 --- a/model/textparse/promparse.go +++ b/model/textparse/promparse.go @@ -229,7 +229,9 @@ func (p *PromParser) Comment() []byte { // Labels writes the labels of the current sample into the passed labels. func (p *PromParser) Labels(l *labels.Labels) { - s := yoloString(p.series) + // Defensive copy in case the following keeps a reference. + // See https://github.com/prometheus/prometheus/issues/16490 + s := string(p.series) p.builder.Reset() metricName := unreplace(s[p.offsets[0]-p.start : p.offsets[1]-p.start])