1
0
mirror of https://github.com/golang/go synced 2024-11-21 23:44:39 -07:00

image/png: check zlib checksum during Decode

R=nigeltao
CC=golang-dev
https://golang.org/cl/4987041
This commit is contained in:
Russ Cox 2011-09-07 13:23:16 -04:00
parent f2460a8c57
commit 299f524d90
7 changed files with 41 additions and 4 deletions

View File

@ -489,6 +489,16 @@ func (d *decoder) idatReader(idat io.Reader) (image.Image, os.Error) {
// The current row for y is the previous row for y+1.
pr, cr = cr, pr
}
// Check for EOF, to verify the zlib checksum.
n, err := r.Read(pr[:1])
if err != os.EOF {
return nil, FormatError(err.String())
}
if n != 0 {
return nil, FormatError("too much pixel data")
}
return img, nil
}

View File

@ -10,6 +10,7 @@ import (
"image"
"io"
"os"
"strings"
"testing"
)
@ -41,7 +42,7 @@ var filenamesShort = []string{
"basn6a16",
}
func readPng(filename string) (image.Image, os.Error) {
func readPNG(filename string) (image.Image, os.Error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
@ -183,7 +184,7 @@ func TestReader(t *testing.T) {
}
for _, fn := range names {
// Read the .png file.
img, err := readPng("testdata/pngsuite/" + fn + ".png")
img, err := readPNG("testdata/pngsuite/" + fn + ".png")
if err != nil {
t.Error(fn, err)
continue
@ -239,3 +240,29 @@ func TestReader(t *testing.T) {
}
}
}
var readerErrors = []struct {
file string
err string
}{
{"invalid-zlib.png", "zlib checksum error"},
{"invalid-crc32.png", "invalid checksum"},
{"invalid-noend.png", "unexpected EOF"},
{"invalid-trunc.png", "unexpected EOF"},
}
func TestReaderError(t *testing.T) {
for _, tt := range readerErrors {
img, err := readPNG("testdata/" + tt.file)
if err == nil {
t.Errorf("decoding %s: missing error", tt.file)
continue
}
if !strings.Contains(err.String(), tt.err) {
t.Errorf("decoding %s: %s, want %s", tt.file, err, tt.err)
}
if img != nil {
t.Errorf("decoding %s: have image + error")
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -56,13 +56,13 @@ func TestWriter(t *testing.T) {
for _, fn := range names {
qfn := "testdata/pngsuite/" + fn + ".png"
// Read the image.
m0, err := readPng(qfn)
m0, err := readPNG(qfn)
if err != nil {
t.Error(fn, err)
continue
}
// Read the image again, encode it, and decode it.
m1, err := readPng(qfn)
m1, err := readPNG(qfn)
if err != nil {
t.Error(fn, err)
return