Skip to content

Commit

Permalink
tiff, bmp: return io.UnexpectedEOF on empty data
Browse files Browse the repository at this point in the history
Check for an EOF when decoding the tiff and bmp headers,
so that decoding empty data does not return an EOF.

Fixes golang/go#11391

Change-Id: I73394b9dcc0d2e4e2a7bc56addb3f690791820ba
GitHub-Last-Rev: 924636fca185e34399b558c92c1d4b80f43eadfc
GitHub-Pull-Request: golang/image#3
Reviewed-on: https://go-review.googlesource.com/c/image/+/205458
Run-TryBot: Akhil Indurti <aindurti@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Akhil Indurti <aindurti@gmail.com>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
  • Loading branch information
snapbakkhfbav committed Jan 29, 2024
1 parent 5167578 commit 98e6c24
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bmp/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func decodeConfig(r io.Reader) (config image.Config, bitsPerPixel int, topDown b
)
var b [1024]byte
if _, err := io.ReadFull(r, b[:fileHeaderLen+4]); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return image.Config{}, 0, false, err
}
if string(b[:2]) != "BM" {
Expand All @@ -155,6 +158,9 @@ func decodeConfig(r io.Reader) (config image.Config, bitsPerPixel int, topDown b
return image.Config{}, 0, false, ErrUnsupported
}
if _, err := io.ReadFull(r, b[fileHeaderLen+4:fileHeaderLen+infoLen]); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return image.Config{}, 0, false, err
}
width := int(int32(readUint32(b[18:22])))
Expand Down
11 changes: 11 additions & 0 deletions bmp/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package bmp

import (
"bytes"
"fmt"
"image"
"io"
"os"
"testing"

Expand Down Expand Up @@ -75,3 +77,12 @@ func TestDecode(t *testing.T) {
}
}
}

// TestEOF tests that decoding a BMP image returns io.ErrUnexpectedEOF
// when there are no headers or data is empty
func TestEOF(t *testing.T) {
_, err := Decode(bytes.NewReader(nil))
if err != io.ErrUnexpectedEOF {
t.Errorf("Error should be io.ErrUnexpectedEOF on nil but got %v", err)
}
}
3 changes: 3 additions & 0 deletions tiff/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ func newDecoder(r io.Reader) (*decoder, error) {

p := make([]byte, 8)
if _, err := d.r.ReadAt(p, 0); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, err
}
switch string(p[0:4]) {
Expand Down
10 changes: 10 additions & 0 deletions tiff/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/hex"
"errors"
"image"
"io"
"io/ioutil"
"os"
"strings"
Expand Down Expand Up @@ -193,6 +194,15 @@ func TestDecodeLZW(t *testing.T) {
compare(t, img0, img1)
}

// TestEOF tests that decoding a TIFF image returns io.ErrUnexpectedEOF
// when there are no headers or data is empty
func TestEOF(t *testing.T) {
_, err := Decode(bytes.NewReader(nil))
if err != io.ErrUnexpectedEOF {
t.Errorf("Error should be io.ErrUnexpectedEOF on nil but got %v", err)
}
}

// TestDecodeCCITT tests that decoding a PNG image and a CCITT compressed TIFF
// image result in the same pixel data.
func TestDecodeCCITT(t *testing.T) {
Expand Down

0 comments on commit 98e6c24

Please sign in to comment.