1
0
mirror of https://github.com/golang/go synced 2024-11-18 19:54:44 -07:00

image/jpeg: handle Read returning n > 0, err != nil in d.fill

Fixes #9127.

LGTM=r
R=bradfitz, r
CC=golang-codereviews, nigeltao
https://golang.org/cl/178120043
This commit is contained in:
Russ Cox 2014-11-22 13:55:33 -05:00
parent 8cda58c25e
commit 04923042bd
2 changed files with 49 additions and 0 deletions

View File

@ -143,6 +143,9 @@ func (d *decoder) fill() error {
// Fill in the rest of the buffer.
n, err := d.r.Read(d.bytes.buf[d.bytes.j:])
d.bytes.j += n
if n > 0 {
err = nil
}
return err
}

View File

@ -9,6 +9,7 @@ import (
"fmt"
"image"
"image/color"
"io"
"io/ioutil"
"math/rand"
"os"
@ -88,6 +89,51 @@ func decodeFile(filename string) (image.Image, error) {
return Decode(f)
}
type eofReader struct {
data []byte // deliver from Read without EOF
dataEOF []byte // then deliver from Read with EOF on last chunk
lenAtEOF int
}
func (r *eofReader) Read(b []byte) (n int, err error) {
if len(r.data) > 0 {
n = copy(b, r.data)
r.data = r.data[n:]
} else {
n = copy(b, r.dataEOF)
r.dataEOF = r.dataEOF[n:]
if len(r.dataEOF) == 0 {
err = io.EOF
if r.lenAtEOF == -1 {
r.lenAtEOF = n
}
}
}
return
}
func TestDecodeEOF(t *testing.T) {
// Check that if reader returns final data and EOF at same time, jpeg handles it.
data, err := ioutil.ReadFile("../testdata/video-001.jpeg")
if err != nil {
t.Fatal(err)
}
n := len(data)
for i := 0; i < n; {
r := &eofReader{data[:n-i], data[n-i:], -1}
_, err := Decode(r)
if err != nil {
t.Errorf("Decode with Read() = %d, EOF: %v", r.lenAtEOF, err)
}
if i == 0 {
i = 1
} else {
i *= 2
}
}
}
// check checks that the two pix data are equal, within the given bounds.
func check(bounds image.Rectangle, pix0, pix1 []byte, stride0, stride1 int) error {
if stride0 <= 0 || stride0%8 != 0 {