internal/png: update for Go 1.18

This commit is contained in:
Hajime Hoshi 2022-04-06 11:32:48 +09:00
parent bb2cd3059b
commit 6e19c793f6
3 changed files with 34 additions and 70 deletions

View File

@ -25,37 +25,48 @@ import (
"go/token" "go/token"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/ast/astutil"
) )
func run() error { func pngDir() (string, error) {
// TODO: Use go/packages with specifying build tags so that stdlibfuzz can be avoided. dir, err := exec.Command("go", "list", "-f", "{{.Dir}}", "image/png").Output()
dir := filepath.Join(runtime.GOROOT(), "src", "image", "png") 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 { if err != nil {
return err return err
} }
for _, f := range files { for _, f := range files {
if f.IsDir() { in, err := os.Open(filepath.Join(dir, f))
continue
}
if strings.HasSuffix(f.Name(), "_test.go") {
continue
}
in, err := os.Open(filepath.Join(dir, f.Name()))
if err != nil { if err != nil {
return err return err
} }
defer in.Close() defer in.Close()
out, err := os.Create("stdlib" + f.Name()) out, err := os.Create("stdlib" + f)
if err != nil { if err != nil {
return err return err
} }

View File

@ -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
}

View File

@ -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 dstPix, stride, rect = target.Pix, target.Stride, target.Rect
bytesPerPixel = 8 bytesPerPixel = 8
case *image.Paletted: case *image.Paletted:
srcPix = src.(*image.Paletted).Pix source := src.(*image.Paletted)
srcPix = source.Pix
dstPix, stride, rect = target.Pix, target.Stride, target.Rect dstPix, stride, rect = target.Pix, target.Stride, target.Rect
bytesPerPixel = 1 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: case *image.RGBA:
srcPix = src.(*image.RGBA).Pix srcPix = src.(*image.RGBA).Pix
dstPix, stride, rect = target.Pix, target.Stride, target.Rect dstPix, stride, rect = target.Pix, target.Stride, target.Rect