From fc22d9fe39eda98eec04ad1a2ee270a88e570e45 Mon Sep 17 00:00:00 2001 From: GalaxyForcew Date: Tue, 24 Jan 2023 15:04:02 +0800 Subject: [PATCH] tiff: add Fuzz function This change adds a sample Fuzz test function to package tiff, under the gofuzz build tag. The function is based on the tiff/tiff.go code, from github.com/dvyukov/go-fuzz-corpus. Fixes golang/go#30719 Updates golang/go#19109 Change-Id: I78771e9a1bd01651ba6ca421ba41f0c0e95d0c53 Reviewed-on: https://go-review.googlesource.com/c/image/+/167097 Run-TryBot: Dmitry Vyukov TryBot-Result: Gobot Gobot Reviewed-by: thepudds Reviewed-by: Josh Bleecher Snyder Reviewed-by: Dmitry Vyukov --- tiff/fuzz.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tiff/fuzz.go diff --git a/tiff/fuzz.go b/tiff/fuzz.go new file mode 100644 index 00000000..ec52c788 --- /dev/null +++ b/tiff/fuzz.go @@ -0,0 +1,29 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build gofuzz + +package tiff + +import "bytes" + +func Fuzz(data []byte) int { + cfg, err := DecodeConfig(bytes.NewReader(data)) + if err != nil { + return 0 + } + if cfg.Width*cfg.Height > 1e6 { + return 0 + } + img, err := Decode(bytes.NewReader(data)) + if err != nil { + return 0 + } + var w bytes.Buffer + err = Encode(&w, img, nil) + if err != nil { + panic(err) + } + return 1 +}