Skip to content

Commit

Permalink
Merge pull request #39 from mfrw/fuzz
Browse files Browse the repository at this point in the history
Issue #30: add a basic fuzz test
  • Loading branch information
valyala committed Aug 15, 2019
2 parents 73ca3ce + 93b35b0 commit 4864f0c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -102,6 +102,21 @@ See also [examples](https://godoc.org/github.com/valyala/fastjson#pkg-examples).
* Prefer iterating over array returned from [Value.GetArray](https://godoc.org/github.com/valyala/fastjson#Object.Visit)
with a range loop instead of calling `Value.Get*` for each array item.

## Fuzzing
Install [go-fuzz](https://github.com/dvyukov/go-fuzz) & optionally the go-fuzz-corpus.

```bash
go get -u github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-build
```

Build using `go-fuzz-build` and run `go-fuzz` with an optional corpus.

```bash
mkdir -p workdir/corpus
cp $GOPATH/src/github.com/dvyukov/go-fuzz-corpus/json/corpus/* workdir/corpus
go-fuzz-build github.com/valyala/fastjson
go-fuzz -bin=fastjson-fuzz.zip -workdir=workdir
```

## Benchmarks

Expand Down
22 changes: 22 additions & 0 deletions fuzz.go
@@ -0,0 +1,22 @@
// +build gofuzz

package fastjson

func Fuzz(data []byte) int {
err := ValidateBytes(data)
if err != nil {
return 0
}

v := MustParseBytes(data)

dst := make([]byte, 0)
dst = v.MarshalTo(dst)

err = ValidateBytes(dst)
if err != nil {
panic(err)
}

return 1
}

0 comments on commit 4864f0c

Please sign in to comment.