Skip to content

Commit 3403ee5

Browse files
zuerchernigeltao
authored andcommitted
image/png: fix palette extension to handle 255 color images
The PNG decode path attempts to handle paletted images that refer to non-existent palette indicies. This PR fixes a corner case were images that have exactly 255 palette colors and an IDAT chunk that references palette index 255 produce an invalid image such that invoking At on the pixel(s) in question causes an index out of range panic. Fixes #31830 Change-Id: I34c44d9de5b9d76fe8c45c04e866fbc7f51f2a9c Reviewed-on: https://go-review.googlesource.com/c/go/+/175397 Run-TryBot: Josh Bleecher Snyder <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Nigel Tao <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 503e6cc commit 3403ee5

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/image/png/reader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ func (d *decoder) readImagePass(r io.Reader, pass int, allocateOnly bool) (image
706706
}
707707
}
708708
case cbP8:
709-
if len(paletted.Palette) != 255 {
709+
if len(paletted.Palette) != 256 {
710710
for x := 0; x < width; x++ {
711711
if len(paletted.Palette) <= int(cdat[x]) {
712712
paletted.Palette = paletted.Palette[:int(cdat[x])+1]

src/image/png/reader_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,21 @@ func TestUnknownChunkLengthUnderflow(t *testing.T) {
584584
}
585585
}
586586

587+
func TestPaletted8OutOfRangePixel(t *testing.T) {
588+
// IDAT contains a reference to a palette index that does not exist in the file.
589+
img, err := readPNG("testdata/invalid-palette.png")
590+
if err != nil {
591+
t.Errorf("decoding invalid-palette.png: unexpected error %v", err)
592+
return
593+
}
594+
595+
// Expect that the palette is extended with opaque black.
596+
want := color.RGBA{0x00, 0x00, 0x00, 0xff}
597+
if got := img.At(15, 15); got != want {
598+
t.Errorf("got %F %v, expected %T %v", got, got, want, want)
599+
}
600+
}
601+
587602
func TestGray8Transparent(t *testing.T) {
588603
// These bytes come from https://golang.org/issues/19553
589604
m, err := Decode(bytes.NewReader([]byte{
1.1 KB
Loading

0 commit comments

Comments
 (0)