chai2010 / guetzli-go

Guetzli perceptual JPEG encoder for Go (Zero Dependencies).
https://godoc.org/github.com/chai2010/guetzli-go
85 stars 7 forks source link

panic: runtime error: index out of range #11

Open bayandin opened 5 years ago

bayandin commented 5 years ago

I'm playing with go-fuzz and have found a couple of crashes:

package main

import (
    "bytes"
    "image/jpeg"

    "github.com/chai2010/guetzli-go"
)

func main() {
    data := []byte("\xff\xd8\xff\xc2\x00\v\b00\x00\x00\x01\x01\x11\x03\xff\xda\x00\b\x01\x01000\x01\xff\xd9")

    img, err := jpeg.Decode(bytes.NewReader(data))
    if err != nil {
        return
    }

    var w bytes.Buffer
    guetzli.Encode(&w, img, &guetzli.Options{Quality: 90})
}

fails with

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/chai2010/guetzli-go.encodeGray(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5a, 0x0, 0x0, 0x0, ...)
    /Users/bayandin/go/src/github.com/chai2010/guetzli-go/cgo.go:23 +0x1b1
github.com/chai2010/guetzli-go.encodeImage(0x4117e00, 0xc00006c040, 0x5a, 0xc000054f10, 0x406ba27, 0xc0000b2000, 0x0)
    /Users/bayandin/go/src/github.com/chai2010/guetzli-go/guetzli.go:135 +0x230
github.com/chai2010/guetzli-go.Encode(0x4117260, 0xc0000b0030, 0x4117e00, 0xc00006c040, 0xc000054f68, 0xc00006c040, 0x0)
    /Users/bayandin/go/src/github.com/chai2010/guetzli-go/guetzli.go:93 +0x70
main.main()
    /Users/bayandin/go/src/github.com/chai2010/guetzli-go/test/test.go:19 +0x15a
exit status 2

and if set chane data to

data := []byte("\xff\xd8\xff\xc2\x00\x11\b00\x00\x00\x03\x01\"\x000\x11\x01\x03\x11\x01\xff\xda\x00\b\x010000\x10\xff\xd9")

then it fails with

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/chai2010/guetzli-go.encodeRGBA(0x41cfee8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5a, 0x0, 0x0, 0x0, ...)
    /Users/bayandin/go/src/github.com/chai2010/guetzli-go/cgo.go:57 +0x1b1
github.com/chai2010/guetzli-go.encodeImage(0x4117e80, 0xc0000be080, 0x5a, 0xc000054f10, 0x406b9f7, 0xc0000b4000, 0x0)
    /Users/bayandin/go/src/github.com/chai2010/guetzli-go/guetzli.go:140 +0x18d
github.com/chai2010/guetzli-go.Encode(0x4117260, 0xc0000921e0, 0x4117e80, 0xc0000be080, 0xc000054f68, 0xc0000be080, 0x0)
    /Users/bayandin/go/src/github.com/chai2010/guetzli-go/guetzli.go:93 +0x70
main.main()
    /Users/bayandin/go/src/github.com/chai2010/guetzli-go/test/test.go:19 +0x168
exit status 2