Skip to content

Commit

Permalink
image/jpeg: return io.ErrUnexpectedEOF on truncated data
Browse files Browse the repository at this point in the history
Decoder calls fill from readFull, ignore and readByte and
readByte did not check returned io.EOF.

This change moves io.EOF translation inside fill.

name                 old speed      new speed      delta
DecodeBaseline-8     67.4MB/s ± 0%  67.3MB/s ± 0%  -0.20%  (p=0.000 n=16+19)
DecodeProgressive-8  43.7MB/s ± 0%  43.6MB/s ± 0%  -0.06%  (p=0.013 n=17+19)

Fixes #56724

Change-Id: Ia0d5cc561f3c2050e25ec3f2b5e6866c3b4941c7
GitHub-Last-Rev: 4701543
GitHub-Pull-Request: #56863
Reviewed-on: https://go-review.googlesource.com/c/go/+/452335
Run-TryBot: Rob Pike <r@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
Reviewed-by: Nigel Tao (INACTIVE; USE @golang.org INSTEAD) <nigeltao@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
AlexanderYastrebov authored and nigeltao committed Feb 24, 2023
1 parent 52fea4b commit f684f3d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/image/jpeg/huffman.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (d *decoder) ensureNBits(n int32) error {
for {
c, err := d.readByteStuffedByte()
if err != nil {
if err == io.EOF {
if err == io.ErrUnexpectedEOF {
return errShortHuffmanData
}
return err
Expand Down
11 changes: 4 additions & 7 deletions src/image/jpeg/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ func (d *decoder) fill() error {
n, err := d.r.Read(d.bytes.buf[d.bytes.j:])
d.bytes.j += n
if n > 0 {
err = nil
return nil
}
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err
}
Expand Down Expand Up @@ -261,9 +264,6 @@ func (d *decoder) readFull(p []byte) error {
break
}
if err := d.fill(); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err
}
}
Expand Down Expand Up @@ -291,9 +291,6 @@ func (d *decoder) ignore(n int) error {
break
}
if err := d.fill(); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/image/jpeg/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,20 @@ func TestExtraneousData(t *testing.T) {
}
}

func TestIssue56724(t *testing.T) {
b, err := os.ReadFile("../testdata/video-001.jpeg")
if err != nil {
t.Fatal(err)
}

b = b[:24] // truncate image data

_, err = Decode(bytes.NewReader(b))
if err != io.ErrUnexpectedEOF {
t.Errorf("got: %v, want: %v", err, io.ErrUnexpectedEOF)
}
}

func benchmarkDecode(b *testing.B, filename string) {
data, err := os.ReadFile(filename)
if err != nil {
Expand Down

0 comments on commit f684f3d

Please sign in to comment.