mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
fix fuzz targets (#5851)
Signed-off-by: Krzysztof Kowalczyk <kkowalczyk@gmail.com>
This commit is contained in:
parent
c9f617af55
commit
6e53980bbf
|
@ -16,7 +16,11 @@
|
||||||
|
|
||||||
package promql
|
package promql
|
||||||
|
|
||||||
import "github.com/prometheus/prometheus/pkg/textparse"
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/prometheus/prometheus/pkg/textparse"
|
||||||
|
)
|
||||||
|
|
||||||
// PromQL parser fuzzing instrumentation for use with
|
// PromQL parser fuzzing instrumentation for use with
|
||||||
// https://github.com/dvyukov/go-fuzz.
|
// https://github.com/dvyukov/go-fuzz.
|
||||||
|
@ -32,7 +36,7 @@ import "github.com/prometheus/prometheus/pkg/textparse"
|
||||||
//
|
//
|
||||||
// Further input samples should go in the folders fuzz-data/ParseMetric/corpus.
|
// Further input samples should go in the folders fuzz-data/ParseMetric/corpus.
|
||||||
//
|
//
|
||||||
// Repeat for ParseMetricSelection, ParseExpr and ParseStmt.
|
// Repeat for FuzzParseOpenMetric, FuzzParseMetricSelector and FuzzParseExpr.
|
||||||
|
|
||||||
// Tuning which value is returned from Fuzz*-functions has a strong influence
|
// Tuning which value is returned from Fuzz*-functions has a strong influence
|
||||||
// on how quick the fuzzer converges on "interesting" cases. At least try
|
// on how quick the fuzzer converges on "interesting" cases. At least try
|
||||||
|
@ -45,22 +49,38 @@ const (
|
||||||
fuzzDiscard = -1
|
fuzzDiscard = -1
|
||||||
)
|
)
|
||||||
|
|
||||||
// Fuzz the metric parser.
|
func fuzzParseMetricWithContentType(in []byte, contentType string) int {
|
||||||
//
|
p := textparse.New(in, contentType)
|
||||||
// Note that his is not the parser for the text-based exposition-format; that
|
var err error
|
||||||
// lives in github.com/prometheus/client_golang/text.
|
for {
|
||||||
func FuzzParseMetric(in []byte) int {
|
_, err = p.Next()
|
||||||
p := textparse.New(in)
|
if err != nil {
|
||||||
for p.Next() {
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err == io.EOF {
|
||||||
|
err = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Err() == nil {
|
if err == nil {
|
||||||
return fuzzInteresting
|
return fuzzInteresting
|
||||||
}
|
}
|
||||||
|
|
||||||
return fuzzMeh
|
return fuzzMeh
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fuzz the metric parser.
|
||||||
|
//
|
||||||
|
// Note that this is not the parser for the text-based exposition-format; that
|
||||||
|
// lives in github.com/prometheus/client_golang/text.
|
||||||
|
func FuzzParseMetric(in []byte) int {
|
||||||
|
return fuzzParseMetricWithContentType(in, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func FuzzParseOpenMetric(in []byte) int {
|
||||||
|
return fuzzParseMetricWithContentType(in, "application/openmetrics-text")
|
||||||
|
}
|
||||||
|
|
||||||
// Fuzz the metric selector parser.
|
// Fuzz the metric selector parser.
|
||||||
func FuzzParseMetricSelector(in []byte) int {
|
func FuzzParseMetricSelector(in []byte) int {
|
||||||
_, err := ParseMetricSelector(string(in))
|
_, err := ParseMetricSelector(string(in))
|
||||||
|
@ -80,13 +100,3 @@ func FuzzParseExpr(in []byte) int {
|
||||||
|
|
||||||
return fuzzMeh
|
return fuzzMeh
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fuzz the parser.
|
|
||||||
func FuzzParseStmts(in []byte) int {
|
|
||||||
_, err := ParseStmts(string(in))
|
|
||||||
if err == nil {
|
|
||||||
return fuzzInteresting
|
|
||||||
}
|
|
||||||
|
|
||||||
return fuzzMeh
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue