Black Lives Matter. Support the Equal Justice Initiative.

Source file src/encoding/json/fuzz.go

Documentation: encoding/json

     1  // Copyright 2019 The Go 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 json
     9  
    10  import (
    11  	"fmt"
    12  )
    13  
    14  func Fuzz(data []byte) (score int) {
    15  	for _, ctor := range []func() interface{}{
    16  		func() interface{} { return new(interface{}) },
    17  		func() interface{} { return new(map[string]interface{}) },
    18  		func() interface{} { return new([]interface{}) },
    19  	} {
    20  		v := ctor()
    21  		err := Unmarshal(data, v)
    22  		if err != nil {
    23  			continue
    24  		}
    25  		score = 1
    26  
    27  		m, err := Marshal(v)
    28  		if err != nil {
    29  			fmt.Printf("v=%#v\n", v)
    30  			panic(err)
    31  		}
    32  
    33  		u := ctor()
    34  		err = Unmarshal(m, u)
    35  		if err != nil {
    36  			fmt.Printf("v=%#v\n", v)
    37  			fmt.Printf("m=%s\n", m)
    38  			panic(err)
    39  		}
    40  	}
    41  
    42  	return
    43  }
    44  

View as plain text