Black Lives Matter. Support the Equal Justice Initiative.

Source file src/image/png/fuzz.go

Documentation: image/png

     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 png
     9  
    10  import (
    11  	"bytes"
    12  	"fmt"
    13  )
    14  
    15  func Fuzz(data []byte) int {
    16  	cfg, err := DecodeConfig(bytes.NewReader(data))
    17  	if err != nil {
    18  		return 0
    19  	}
    20  	if cfg.Width*cfg.Height > 1e6 {
    21  		return 0
    22  	}
    23  	img, err := Decode(bytes.NewReader(data))
    24  	if err != nil {
    25  		return 0
    26  	}
    27  	levels := []CompressionLevel{
    28  		DefaultCompression,
    29  		NoCompression,
    30  		BestSpeed,
    31  		BestCompression,
    32  	}
    33  	for _, l := range levels {
    34  		var w bytes.Buffer
    35  		e := &Encoder{CompressionLevel: l}
    36  		err = e.Encode(&w, img)
    37  		if err != nil {
    38  			panic(err)
    39  		}
    40  		img1, err := Decode(&w)
    41  		if err != nil {
    42  			panic(err)
    43  		}
    44  		got := img1.Bounds()
    45  		want := img.Bounds()
    46  		if !got.Eq(want) {
    47  			fmt.Printf("bounds0: %#v\n", want)
    48  			fmt.Printf("bounds1: %#v\n", got)
    49  			panic("bounds have changed")
    50  		}
    51  	}
    52  	return 1
    53  }
    54  

View as plain text