github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/pgwire/pgwirebase/fuzz.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  // +build gofuzz
    12  
    13  package pgwirebase
    14  
    15  import (
    16  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    18  	"github.com/cockroachdb/cockroach/pkg/util/timeutil"
    19  	"github.com/lib/pq/oid"
    20  )
    21  
    22  var (
    23  	timeCtx = tree.NewParseTimeContext(timeutil.Now())
    24  	// Compile a slice of all oids.
    25  	oids = func() []oid.Oid {
    26  		var ret []oid.Oid
    27  		for oid := range types.OidToType {
    28  			ret = append(ret, oid)
    29  		}
    30  		return ret
    31  	}()
    32  )
    33  
    34  func FuzzDecodeOidDatum(data []byte) int {
    35  	if len(data) < 2 {
    36  		return 0
    37  	}
    38  
    39  	id := oids[int(data[1])%len(oids)]
    40  	code := FormatCode(data[0]) % (FormatBinary + 1)
    41  	b := data[2:]
    42  
    43  	_, err := DecodeOidDatum(timeCtx, id, code, b)
    44  	if err != nil {
    45  		return 0
    46  	}
    47  	return 1
    48  }