Skip to content

Commit 72a6583

Browse files
osocuriosonigeltao
authored andcommitted
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 <[email protected]>
1 parent 95ece43 commit 72a6583

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

tiff/reader.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,13 @@ func Decode(r io.Reader) (img image.Image, err error) {
488488
blocksAcross := 1
489489
blocksDown := 1
490490

491+
if d.config.Width == 0 {
492+
blocksAcross = 0
493+
}
494+
if d.config.Height == 0 {
495+
blocksDown = 0
496+
}
497+
491498
var blockOffsets, blockCounts []uint
492499

493500
if int(d.firstVal(tTileWidth)) != 0 {
@@ -496,8 +503,12 @@ func Decode(r io.Reader) (img image.Image, err error) {
496503
blockWidth = int(d.firstVal(tTileWidth))
497504
blockHeight = int(d.firstVal(tTileLength))
498505

499-
blocksAcross = (d.config.Width + blockWidth - 1) / blockWidth
500-
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
506+
if blockWidth != 0 {
507+
blocksAcross = (d.config.Width + blockWidth - 1) / blockWidth
508+
}
509+
if blockHeight != 0 {
510+
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
511+
}
501512

502513
blockCounts = d.features[tTileByteCounts]
503514
blockOffsets = d.features[tTileOffsets]
@@ -507,7 +518,9 @@ func Decode(r io.Reader) (img image.Image, err error) {
507518
blockHeight = int(d.firstVal(tRowsPerStrip))
508519
}
509520

510-
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
521+
if blockHeight != 0 {
522+
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
523+
}
511524

512525
blockOffsets = d.features[tStripOffsets]
513526
blockCounts = d.features[tStripByteCounts]

tiff/reader_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package tiff
66

77
import (
8+
"bytes"
89
"image"
910
"io/ioutil"
1011
"os"
@@ -162,6 +163,31 @@ func TestDecompress(t *testing.T) {
162163
}
163164
}
164165

166+
// Do not panic when image dimensions are zero, return zero-sized
167+
// image instead.
168+
// Issue 10393.
169+
func TestZeroSizedImages(t *testing.T) {
170+
testsizes := []struct {
171+
w, h int
172+
}{
173+
{0, 0},
174+
{1, 0},
175+
{0, 1},
176+
{1, 1},
177+
}
178+
for _, r := range testsizes {
179+
img := image.NewRGBA(image.Rect(0, 0, r.w, r.h))
180+
var buf bytes.Buffer
181+
if err := Encode(&buf, img, nil); err != nil {
182+
t.Errorf("encode w=%d h=%d: %v", r.w, r.h, err)
183+
continue
184+
}
185+
if _, err := Decode(&buf); err != nil {
186+
t.Errorf("decode w=%d h=%d: %v", r.w, r.h, err)
187+
}
188+
}
189+
}
190+
165191
// benchmarkDecode benchmarks the decoding of an image.
166192
func benchmarkDecode(b *testing.B, filename string) {
167193
b.StopTimer()

0 commit comments

Comments
 (0)