gonum.org/v1/gonum@v0.14.0/graph/formats/dot/fuzz/fuzz.go (about)

     1  // Copyright ©2019 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build gofuzz
     6  // +build gofuzz
     7  
     8  package fuzz
     9  
    10  import (
    11  	"bytes"
    12  	"os/exec"
    13  
    14  	"gonum.org/v1/gonum/graph/formats/dot"
    15  )
    16  
    17  // Fuzz implements the fuzzing function required for go-fuzz.
    18  //
    19  // See documentation at https://github.com/dvyukov/go-fuzz.
    20  func Fuzz(data []byte) int {
    21  	// We don't accept empty data; the dot command does.
    22  	if len(data) == 0 || bytes.Equal(data, []byte{0}) {
    23  		return -1
    24  	}
    25  
    26  	// Check that dot accepts the input without complaint.
    27  	cmd := exec.Command("dot")
    28  	cmd.Stdin = bytes.NewReader(data)
    29  	err := cmd.Run()
    30  	if err != nil {
    31  		return 0
    32  	}
    33  
    34  	// Try to parse the data.
    35  	_, err = dot.Parse(bytes.NewReader(data))
    36  	if err != nil {
    37  		panic("could not parse good dot")
    38  	}
    39  	return 1
    40  }