From 6e19c793f646148008a6d5cf71858e47442eba3e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 6 Apr 2022 11:32:48 +0900 Subject: [PATCH] internal/png: update for Go 1.18 --- internal/png/gen.go | 39 ++++++++++++++++--------- internal/png/stdlibfuzz.go | 55 ------------------------------------ internal/png/stdlibreader.go | 10 ++++++- 3 files changed, 34 insertions(+), 70 deletions(-) delete mode 100644 internal/png/stdlibfuzz.go diff --git a/internal/png/gen.go b/internal/png/gen.go index dad9eadf6..0e7f58550 100644 --- a/internal/png/gen.go +++ b/internal/png/gen.go @@ -25,37 +25,48 @@ import ( "go/token" "io/ioutil" "os" + "os/exec" "path/filepath" - "runtime" "strings" "golang.org/x/tools/go/ast/astutil" ) -func run() error { - // TODO: Use go/packages with specifying build tags so that stdlibfuzz can be avoided. - dir := filepath.Join(runtime.GOROOT(), "src", "image", "png") +func pngDir() (string, error) { + dir, err := exec.Command("go", "list", "-f", "{{.Dir}}", "image/png").Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(dir)), nil +} - files, err := ioutil.ReadDir(dir) +func pngFiles() ([]string, error) { + files, err := exec.Command("go", "list", "-f", `{{join .GoFiles ","}}`, "image/png").Output() + if err != nil { + return nil, err + } + return strings.Split(strings.TrimSpace(string(files)), ","), nil +} + +func run() error { + dir, err := pngDir() + if err != nil { + return err + } + + files, err := pngFiles() if err != nil { return err } for _, f := range files { - if f.IsDir() { - continue - } - if strings.HasSuffix(f.Name(), "_test.go") { - continue - } - - in, err := os.Open(filepath.Join(dir, f.Name())) + in, err := os.Open(filepath.Join(dir, f)) if err != nil { return err } defer in.Close() - out, err := os.Create("stdlib" + f.Name()) + out, err := os.Create("stdlib" + f) if err != nil { return err } diff --git a/internal/png/stdlibfuzz.go b/internal/png/stdlibfuzz.go deleted file mode 100644 index c3b1957f4..000000000 --- a/internal/png/stdlibfuzz.go +++ /dev/null @@ -1,55 +0,0 @@ -// Code generated by gen.go. DO NOT EDIT. - -// 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. - -//go:build gofuzz -// +build gofuzz - -package png - -import ( - "bytes" - "fmt" -) - -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 - } - levels := []CompressionLevel{ - DefaultCompression, - NoCompression, - BestSpeed, - BestCompression, - } - for _, l := range levels { - var w bytes.Buffer - e := &Encoder{CompressionLevel: l} - err = e.Encode(&w, img) - if err != nil { - panic(err) - } - img1, err := Decode(&w) - if err != nil { - panic(err) - } - got := img1.Bounds() - want := img.Bounds() - if !got.Eq(want) { - fmt.Printf("bounds0: %#v\n", want) - fmt.Printf("bounds1: %#v\n", got) - panic("bounds have changed") - } - } - return 1 -} diff --git a/internal/png/stdlibreader.go b/internal/png/stdlibreader.go index 335aff004..0ea16bb50 100644 --- a/internal/png/stdlibreader.go +++ b/internal/png/stdlibreader.go @@ -823,9 +823,17 @@ func (d *decoder) mergePassInto(dst image.Image, src image.Image, pass int) { dstPix, stride, rect = target.Pix, target.Stride, target.Rect bytesPerPixel = 8 case *image.Paletted: - srcPix = src.(*image.Paletted).Pix + source := src.(*image.Paletted) + srcPix = source.Pix dstPix, stride, rect = target.Pix, target.Stride, target.Rect bytesPerPixel = 1 + if len(target.Palette) < len(source.Palette) { + // readImagePass can return a paletted image whose implicit palette + // length (one more than the maximum Pix value) is larger than the + // explicit palette length (what's in the PLTE chunk). Make the + // same adjustment here. + target.Palette = source.Palette + } case *image.RGBA: srcPix = src.(*image.RGBA).Pix dstPix, stride, rect = target.Pix, target.Stride, target.Rect