X Tutup
Skip to content

Commit 1de2267

Browse files
committed
image/png: fix width * height * bpp overflow check.
Previously, the code would only check (w*h), not (w*h*bpp). Fixes golang#22304 Change-Id: Iaca26d916fe4b894d460448c416b1e0b9fd68e44 Reviewed-on: https://go-review.googlesource.com/72350 Reviewed-by: Rob Pike <r@golang.org>
1 parent a31e0a4 commit 1de2267

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/image/png/reader.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func (d *decoder) parseIHDR(length uint32) error {
157157
return FormatError("invalid interlace method")
158158
}
159159
d.interlace = int(d.tmp[12])
160+
160161
w := int32(binary.BigEndian.Uint32(d.tmp[0:4]))
161162
h := int32(binary.BigEndian.Uint32(d.tmp[4:8]))
162163
if w <= 0 || h <= 0 {
@@ -166,6 +167,11 @@ func (d *decoder) parseIHDR(length uint32) error {
166167
if nPixels != int64(int(nPixels)) {
167168
return UnsupportedError("dimension overflow")
168169
}
170+
// There can be up to 8 bytes per pixel, for 16 bits per channel RGBA.
171+
if nPixels != (nPixels*8)/8 {
172+
return UnsupportedError("dimension overflow")
173+
}
174+
169175
d.cb = cbInvalid
170176
d.depth = int(d.tmp[8])
171177
switch d.depth {

src/image/png/reader_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,24 @@ func TestGray8Transparent(t *testing.T) {
649649
}
650650
}
651651

652+
func TestDimensionOverflow(t *testing.T) {
653+
// These bytes come from https://github.com/golang/go/issues/22304
654+
//
655+
// It encodes a 2147483646 × 2147483646 (i.e. 0x7ffffffe × 0x7ffffffe)
656+
// NRGBA image. The (width × height) per se doesn't overflow an int64, but
657+
// (width × height × bytesPerPixel) will.
658+
_, err := Decode(bytes.NewReader([]byte{
659+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
660+
0x7f, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff, 0xfe, 0x08, 0x06, 0x00, 0x00, 0x00, 0x30, 0x57, 0xb3,
661+
0xfd, 0x00, 0x00, 0x00, 0x15, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x62, 0x62, 0x20, 0x12, 0x8c,
662+
0x2a, 0xa4, 0xb3, 0x42, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x13, 0x38, 0x00, 0x15, 0x2d, 0xef,
663+
0x5f, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
664+
}))
665+
if _, ok := err.(UnsupportedError); !ok {
666+
t.Fatalf("Decode: got %v (of type %T), want non-nil error (of type png.UnsupportedError)", err, err)
667+
}
668+
}
669+
652670
func benchmarkDecode(b *testing.B, filename string, bytesPerPixel int) {
653671
data, err := ioutil.ReadFile(filename)
654672
if err != nil {

0 commit comments

Comments
 (0)
X Tutup