Skip to content

Commit

Permalink
Continue on unknown tiff tag type
Browse files Browse the repository at this point in the history
When a tag with unknown tag type is encountered, skip tag and parse
other fields.
  • Loading branch information
op committed Oct 16, 2015
1 parent 709fab3 commit 3f8ff2a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
4 changes: 2 additions & 2 deletions exif/exif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestHugeTagError(t *testing.T) {
}
}

// Check for a 0-length tag value
// Even though the image contains a 0-length tag value, we continue and get some data
func TestZeroLengthTagError(t *testing.T) {
name := filepath.Join(*dataDir, "corrupt/infinite_loop_exif.jpg")
f, err := os.Open(name)
Expand All @@ -196,7 +196,7 @@ func TestZeroLengthTagError(t *testing.T) {
if err == nil {
t.Fatal("no error on bad exif data")
}
if !strings.Contains(err.Error(), "zero length tag value") {
if !strings.Contains(err.Error(), "short read") {
t.Fatal("wrong error:", err.Error())
}
}
36 changes: 36 additions & 0 deletions exif/regress_expected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,42 @@ var regressExpected = map[string]map[FieldName]string{
Contrast: `0`,
FocalLengthIn35mmFilm: `36`,
},
"exif-zero-length.jpg": map[FieldName]string{
ColorSpace: `1`,
ComponentsConfiguration: `""`,
CustomRendered: `0`,
DateTime: `"2010:11:07 13:32:41"`,
DateTimeDigitized: `"2010:11:07 13:32:41"`,
DateTimeOriginal: `"2010:11:07 13:32:41"`,
DigitalZoomRatio: `"100/100"`,
ExifIFDPointer: `182`,
ExifVersion: `"0220"`,
ExposureBiasValue: `"0/65536"`,
ExposureMode: `0`,
ExposureTime: `"257/20000"`,
FNumber: `"28/10"`,
Flash: `16`,
FlashpixVersion: `"0100"`,
FocalLength: `"465/100"`,
ISOSpeedRatings: `50`,
ImageDescription: `""`,
LightSource: `0`,
Make: `"SEMC"`,
MeteringMode: `2`,
Model: `"X10i"`,
Orientation: `1`,
PixelXDimension: `3264`,
PixelYDimension: `2448`,
ResolutionUnit: `2`,
SceneCaptureType: `0`,
ShutterSpeedValue: `"411787/65536"`,
ThumbJPEGInterchangeFormat: `670`,
ThumbJPEGInterchangeFormatLength: `6101`,
WhiteBalance: `0`,
XResolution: `"72/1"`,
YCbCrPositioning: `2`,
YResolution: `"72/1"`,
},
"FailedHash-NoDate-sep-remembory.jpg": map[FieldName]string{
Model: `"MFC-7840W"`,
YResolution: `"150/1"`,
Expand Down
Binary file added exif/samples/exif-zero-length.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 13 additions & 4 deletions tiff/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const (
OtherVal
)

var ErrShortReadTagValue = errors.New("tiff: short read of tag value")
var (
ErrShortReadTagValue = errors.New("tiff: short read of tag value")
errUnhandledTagType = errors.New("tiff: unhandled tag type")
)

var formatNames = map[Format]string{
IntVal: "int",
Expand Down Expand Up @@ -138,11 +141,17 @@ func DecodeTag(r ReadAtReader, order binary.ByteOrder) (*Tag, error) {
return t, errors.New("invalid Count offset in tag")
}

valLen := typeSize[t.Type] * t.Count
if valLen == 0 {
return t, errors.New("zero length tag value")
// Ignore the value/offset if we don't know about the size of the type.
size, ok := typeSize[t.Type]
if !ok {
var ignore [4]byte
if _, err = io.ReadFull(r, ignore[:]); err != nil {
return t, errors.New("tiff: unknown tag offset read failed: " + err.Error())
}
return nil, errUnhandledTagType
}

valLen := size * t.Count
if valLen > 4 {
binary.Read(r, order, &t.ValOffset)

Expand Down
4 changes: 3 additions & 1 deletion tiff/tiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ func DecodeDir(r ReadAtReader, order binary.ByteOrder) (d *Dir, offset int32, er
// load tags
for n := 0; n < int(nTags); n++ {
t, err := DecodeTag(r, order)
if err != nil {
if err == errUnhandledTagType {
continue
} else if err != nil {
return nil, 0, err
}
d.Tags = append(d.Tags, t)
Expand Down

0 comments on commit 3f8ff2a

Please sign in to comment.