fix fuzz targets (#5851)

Signed-off-by: Krzysztof Kowalczyk <kkowalczyk@gmail.com>
This commit is contained in:
Krzysztof Kowalczyk 2019-08-12 11:47:57 -07:00 committed by Julius Volz
parent c9f617af55
commit 6e53980bbf
1 changed files with 30 additions and 20 deletions

View File

@ -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
}