x/image/tiff: fix division by zero when decoding empty image
Return a zero width or zero height image of the appropriate type. Fixes golang/go#10393. Change-Id: I3aa7b6125447726600fb072ce1e8682373ce2a57 Reviewed-on: https://go-review.googlesource.com/9182 Reviewed-by: Nigel Tao <nigeltao@golang.org>
This commit is contained in:
parent
95ece4346f
commit
72a6583050
|
@ -488,6 +488,13 @@ func Decode(r io.Reader) (img image.Image, err error) {
|
||||||
blocksAcross := 1
|
blocksAcross := 1
|
||||||
blocksDown := 1
|
blocksDown := 1
|
||||||
|
|
||||||
|
if d.config.Width == 0 {
|
||||||
|
blocksAcross = 0
|
||||||
|
}
|
||||||
|
if d.config.Height == 0 {
|
||||||
|
blocksDown = 0
|
||||||
|
}
|
||||||
|
|
||||||
var blockOffsets, blockCounts []uint
|
var blockOffsets, blockCounts []uint
|
||||||
|
|
||||||
if int(d.firstVal(tTileWidth)) != 0 {
|
if int(d.firstVal(tTileWidth)) != 0 {
|
||||||
|
@ -496,8 +503,12 @@ func Decode(r io.Reader) (img image.Image, err error) {
|
||||||
blockWidth = int(d.firstVal(tTileWidth))
|
blockWidth = int(d.firstVal(tTileWidth))
|
||||||
blockHeight = int(d.firstVal(tTileLength))
|
blockHeight = int(d.firstVal(tTileLength))
|
||||||
|
|
||||||
|
if blockWidth != 0 {
|
||||||
blocksAcross = (d.config.Width + blockWidth - 1) / blockWidth
|
blocksAcross = (d.config.Width + blockWidth - 1) / blockWidth
|
||||||
|
}
|
||||||
|
if blockHeight != 0 {
|
||||||
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
|
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
|
||||||
|
}
|
||||||
|
|
||||||
blockCounts = d.features[tTileByteCounts]
|
blockCounts = d.features[tTileByteCounts]
|
||||||
blockOffsets = d.features[tTileOffsets]
|
blockOffsets = d.features[tTileOffsets]
|
||||||
|
@ -507,7 +518,9 @@ func Decode(r io.Reader) (img image.Image, err error) {
|
||||||
blockHeight = int(d.firstVal(tRowsPerStrip))
|
blockHeight = int(d.firstVal(tRowsPerStrip))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if blockHeight != 0 {
|
||||||
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
|
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
|
||||||
|
}
|
||||||
|
|
||||||
blockOffsets = d.features[tStripOffsets]
|
blockOffsets = d.features[tStripOffsets]
|
||||||
blockCounts = d.features[tStripByteCounts]
|
blockCounts = d.features[tStripByteCounts]
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
package tiff
|
package tiff
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"image"
|
"image"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
@ -162,6 +163,31 @@ func TestDecompress(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do not panic when image dimensions are zero, return zero-sized
|
||||||
|
// image instead.
|
||||||
|
// Issue 10393.
|
||||||
|
func TestZeroSizedImages(t *testing.T) {
|
||||||
|
testsizes := []struct {
|
||||||
|
w, h int
|
||||||
|
}{
|
||||||
|
{0, 0},
|
||||||
|
{1, 0},
|
||||||
|
{0, 1},
|
||||||
|
{1, 1},
|
||||||
|
}
|
||||||
|
for _, r := range testsizes {
|
||||||
|
img := image.NewRGBA(image.Rect(0, 0, r.w, r.h))
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := Encode(&buf, img, nil); err != nil {
|
||||||
|
t.Errorf("encode w=%d h=%d: %v", r.w, r.h, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := Decode(&buf); err != nil {
|
||||||
|
t.Errorf("decode w=%d h=%d: %v", r.w, r.h, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// benchmarkDecode benchmarks the decoding of an image.
|
// benchmarkDecode benchmarks the decoding of an image.
|
||||||
func benchmarkDecode(b *testing.B, filename string) {
|
func benchmarkDecode(b *testing.B, filename string) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user