Skip to content

Commit ba8fa0e

Browse files
committed
image/png: don't silently swallow io.ReadFull's io.EOF error when it
lands exactly on an IDAT row boundary. Fixes #10493 Change-Id: I12be7c5bdcde7032e17ed1d4400db5f17c72bc87 Reviewed-on: https://go-review.googlesource.com/9270 Reviewed-by: Rob Pike <[email protected]>
1 parent 133966d commit ba8fa0e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/image/png/reader.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,15 @@ func (d *decoder) decode() (image.Image, error) {
326326
var img image.Image
327327
if d.interlace == itNone {
328328
img, err = d.readImagePass(r, 0, false)
329+
if err != nil {
330+
return nil, err
331+
}
329332
} else if d.interlace == itAdam7 {
330333
// Allocate a blank image of the full size.
331334
img, err = d.readImagePass(nil, 0, true)
335+
if err != nil {
336+
return nil, err
337+
}
332338
for pass := 0; pass < 7; pass++ {
333339
imagePass, err := d.readImagePass(r, pass, false)
334340
if err != nil {
@@ -430,6 +436,9 @@ func (d *decoder) readImagePass(r io.Reader, pass int, allocateOnly bool) (image
430436
// Read the decompressed bytes.
431437
_, err := io.ReadFull(r, cr)
432438
if err != nil {
439+
if err == io.EOF || err == io.ErrUnexpectedEOF {
440+
return nil, FormatError("not enough pixel data")
441+
}
433442
return nil, err
434443
}
435444

src/image/png/reader_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,21 @@ func TestPalettedDecodeConfig(t *testing.T) {
320320
}
321321
}
322322

323+
func TestIncompleteIDATOnRowBoundary(t *testing.T) {
324+
// The following is an invalid 1x2 grayscale PNG image. The header is OK,
325+
// but the zlib-compressed IDAT payload contains two bytes "\x02\x00",
326+
// which is only one row of data (the leading "\x02" is a row filter).
327+
const (
328+
ihdr = "\x00\x00\x00\x0dIHDR\x00\x00\x00\x01\x00\x00\x00\x02\x08\x00\x00\x00\x00\xbc\xea\xe9\xfb"
329+
idat = "\x00\x00\x00\x0eIDAT\x78\x9c\x62\x62\x00\x04\x00\x00\xff\xff\x00\x06\x00\x03\xfa\xd0\x59\xae"
330+
iend = "\x00\x00\x00\x00IEND\xae\x42\x60\x82"
331+
)
332+
_, err := Decode(strings.NewReader(pngHeader + ihdr + idat + iend))
333+
if err == nil {
334+
t.Fatal("got nil error, want non-nil")
335+
}
336+
}
337+
323338
func TestMultipletRNSChunks(t *testing.T) {
324339
/*
325340
The following is a valid 1x1 paletted PNG image with a 1-element palette

0 commit comments

Comments
 (0)