Skip to content

Commit

Permalink
tiff/lzw: should return io.UnexpectedEOF on empty data
Browse files Browse the repository at this point in the history
bmp: should return io.UnexpectedEOF on empty data

fixing: golang/go#11391
  • Loading branch information
Sardorbek Pulatov committed Nov 5, 2019
1 parent e7c1f5e commit 50c5072
Show file tree
Hide file tree
Showing 4 changed files with 32 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
12 changes: 12 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,13 @@ 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([]byte{}))

if err != io.ErrUnexpectedEOF {
t.Errorf("Error should be io.ErrUnexpectedEOF 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
11 changes: 11 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,16 @@ 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([]byte{}))

if err != io.ErrUnexpectedEOF {
t.Errorf("Error should be io.ErrUnexpectedEOF 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 50c5072

Please sign in to comment.