Skip to content

Commit

Permalink
fuzz: move fuzz_target from oss-fuzz (#861)
Browse files Browse the repository at this point in the history
  • Loading branch information
manunio committed Apr 29, 2023
1 parent 654811f commit 2aa0836
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ cover() {

pushd "$dir"
go test -covermode=atomic -coverpkg=./... -coverprofile=coverage.out.tmp ./...
cat coverage.out.tmp | grep -v testsuite | grep -v tomltestgen | grep -v gotoml-test-decoder > coverage.out
cat coverage.out.tmp | grep -v fuzz | grep -v testsuite | grep -v tomltestgen | grep -v gotoml-test-decoder > coverage.out
go tool cover -func=coverage.out
popd

Expand Down
45 changes: 45 additions & 0 deletions ossfuzz/fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build go1.18 || go1.19 || go1.20
// +build go1.18 go1.19 go1.20

package ossfuzz

import (
"fmt"
"reflect"
"strings"

"github.com/pelletier/go-toml/v2"
)

func FuzzToml(data []byte) int {
if len(data) >= 10240 {
return 0
}

if strings.Contains(string(data), "nan") {
return 0
}

var v interface{}
err := toml.Unmarshal(data, &v)
if err != nil {
return 0
}

encoded, err := toml.Marshal(v)
if err != nil {
panic(fmt.Sprintf("failed to marshal unmarshaled document: %s", err))
}

var v2 interface{}
err = toml.Unmarshal(encoded, &v2)
if err != nil {
panic(fmt.Sprintf("failed round trip: %s", err))
}

if !reflect.DeepEqual(v, v2) {
panic(fmt.Sprintf("not equal: %#+v %#+v", v, v2))
}

return 1
}

0 comments on commit 2aa0836

Please sign in to comment.