Source file src/encoding/json/fuzz.go

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

View as plain text