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:
Håvard Haugen 2015-04-21 00:05:38 +02:00 committed by Nigel Tao
parent 95ece4346f
commit 72a6583050
2 changed files with 42 additions and 3 deletions

View File

@ -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))
blocksAcross = (d.config.Width + blockWidth - 1) / blockWidth if blockWidth != 0 {
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight blocksAcross = (d.config.Width + blockWidth - 1) / blockWidth
}
if blockHeight != 0 {
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))
} }
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight if blockHeight != 0 {
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
}
blockOffsets = d.features[tStripOffsets] blockOffsets = d.features[tStripOffsets]
blockCounts = d.features[tStripByteCounts] blockCounts = d.features[tStripByteCounts]

View File

@ -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()