...

Source file src/github.com/pelletier/go-toml/v2/ossfuzz/fuzz.go

Documentation: github.com/pelletier/go-toml/v2/ossfuzz

     1  //go:build go1.18 || go1.19 || go1.20 || go1.21
     2  // +build go1.18 go1.19 go1.20 go1.21
     3  
     4  package ossfuzz
     5  
     6  import (
     7  	"fmt"
     8  	"reflect"
     9  	"strings"
    10  
    11  	"github.com/pelletier/go-toml/v2"
    12  )
    13  
    14  func FuzzToml(data []byte) int {
    15  	if len(data) >= 2048 {
    16  		return 0
    17  	}
    18  
    19  	if strings.Contains(string(data), "nan") {
    20  		return 0
    21  	}
    22  
    23  	var v interface{}
    24  	err := toml.Unmarshal(data, &v)
    25  	if err != nil {
    26  		return 0
    27  	}
    28  
    29  	encoded, err := toml.Marshal(v)
    30  	if err != nil {
    31  		panic(fmt.Sprintf("failed to marshal unmarshaled document: %s", err))
    32  	}
    33  
    34  	var v2 interface{}
    35  	err = toml.Unmarshal(encoded, &v2)
    36  	if err != nil {
    37  		panic(fmt.Sprintf("failed round trip: %s", err))
    38  	}
    39  
    40  	if !reflect.DeepEqual(v, v2) {
    41  		panic(fmt.Sprintf("not equal: %#+v %#+v", v, v2))
    42  	}
    43  
    44  	return 1
    45  }
    46  

View as plain text