File tree 2 files changed +42
-3
lines changed
2 files changed +42
-3
lines changed Original file line number Diff line number Diff line change @@ -488,6 +488,13 @@ func Decode(r io.Reader) (img image.Image, err error) {
488
488
blocksAcross := 1
489
489
blocksDown := 1
490
490
491
+ if d .config .Width == 0 {
492
+ blocksAcross = 0
493
+ }
494
+ if d .config .Height == 0 {
495
+ blocksDown = 0
496
+ }
497
+
491
498
var blockOffsets , blockCounts []uint
492
499
493
500
if int (d .firstVal (tTileWidth )) != 0 {
@@ -496,8 +503,12 @@ func Decode(r io.Reader) (img image.Image, err error) {
496
503
blockWidth = int (d .firstVal (tTileWidth ))
497
504
blockHeight = int (d .firstVal (tTileLength ))
498
505
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
+ }
501
512
502
513
blockCounts = d .features [tTileByteCounts ]
503
514
blockOffsets = d .features [tTileOffsets ]
@@ -507,7 +518,9 @@ func Decode(r io.Reader) (img image.Image, err error) {
507
518
blockHeight = int (d .firstVal (tRowsPerStrip ))
508
519
}
509
520
510
- blocksDown = (d .config .Height + blockHeight - 1 ) / blockHeight
521
+ if blockHeight != 0 {
522
+ blocksDown = (d .config .Height + blockHeight - 1 ) / blockHeight
523
+ }
511
524
512
525
blockOffsets = d .features [tStripOffsets ]
513
526
blockCounts = d .features [tStripByteCounts ]
Original file line number Diff line number Diff line change 5
5
package tiff
6
6
7
7
import (
8
+ "bytes"
8
9
"image"
9
10
"io/ioutil"
10
11
"os"
@@ -162,6 +163,31 @@ func TestDecompress(t *testing.T) {
162
163
}
163
164
}
164
165
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
+
165
191
// benchmarkDecode benchmarks the decoding of an image.
166
192
func benchmarkDecode (b * testing.B , filename string ) {
167
193
b .StopTimer ()
You can’t perform that action at this time.
0 commit comments