Commit 299f524d authored by Russ Cox's avatar Russ Cox

image/png: check zlib checksum during Decode

R=nigeltao
CC=golang-dev
https://golang.org/cl/4987041
parent f2460a8c
......@@ -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
}
......
......@@ -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")
}
}
}
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment