From 6e53980bbf8bbd11c1b54ca3fd69bade7e31a778 Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Mon, 12 Aug 2019 11:47:57 -0700 Subject: [PATCH] fix fuzz targets (#5851) Signed-off-by: Krzysztof Kowalczyk --- promql/fuzz.go | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/promql/fuzz.go b/promql/fuzz.go index 4518280af..6b4e3a4fe 100644 --- a/promql/fuzz.go +++ b/promql/fuzz.go @@ -16,7 +16,11 @@ package promql -import "github.com/prometheus/prometheus/pkg/textparse" +import ( + "io" + + "github.com/prometheus/prometheus/pkg/textparse" +) // PromQL parser fuzzing instrumentation for use with // 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. // -// Repeat for ParseMetricSelection, ParseExpr and ParseStmt. +// Repeat for FuzzParseOpenMetric, FuzzParseMetricSelector and FuzzParseExpr. // Tuning which value is returned from Fuzz*-functions has a strong influence // on how quick the fuzzer converges on "interesting" cases. At least try @@ -45,22 +49,38 @@ const ( fuzzDiscard = -1 ) -// Fuzz the metric parser. -// -// Note that his is not the parser for the text-based exposition-format; that -// lives in github.com/prometheus/client_golang/text. -func FuzzParseMetric(in []byte) int { - p := textparse.New(in) - for p.Next() { +func fuzzParseMetricWithContentType(in []byte, contentType string) int { + p := textparse.New(in, contentType) + var err error + for { + _, err = p.Next() + if err != nil { + break + } + } + if err == io.EOF { + err = nil } - if p.Err() == nil { + if err == nil { return fuzzInteresting } 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. func FuzzParseMetricSelector(in []byte) int { _, err := ParseMetricSelector(string(in)) @@ -80,13 +100,3 @@ func FuzzParseExpr(in []byte) int { return fuzzMeh } - -// Fuzz the parser. -func FuzzParseStmts(in []byte) int { - _, err := ParseStmts(string(in)) - if err == nil { - return fuzzInteresting - } - - return fuzzMeh -}