Skip to content

Instantly share code, notes, and snippets.

@chai2010
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save chai2010/fc20e1af87e10de51e41 to your computer and use it in GitHub Desktop.

Select an option

Save chai2010/fc20e1af87e10de51e41 to your computer and use it in GitHub Desktop.
x/image/tiff: test

// golang/go#10597 // test

func TestDecodeInvalidDataType(t *testing.T) {
	b, err := ioutil.ReadFile("../testdata/bw-uncompressed.tiff")
	if err != nil {
		t.Fatal(err)
	}

	// tiffinfo:
	// https://github.com/chai2010/tiff/blob/master/apps/tiffinfo.go
	//
	// go run tiffinfo.go ../testdata/bw-uncompressed.tiff:
	//
	//	tiff.Header{
	//		ByteOrder:LittleEndian
	//		TiffType:TiffType_ClassicTIFF
	//		Offset:0x00000454
	//	}
	//	tiff.IFD(0x00000454) {
	//		TagType_NewSubfileType(DataType_Long): TagValue_NewSubfileType_Nil
	//		TagType_ImageWidth(DataType_Short): 153
	//		TagType_ImageLength(DataType_Short): 55
	//		TagType_BitsPerSample(DataType_Short): [1]
	//		TagType_Compression(DataType_Short): TagValue_CompressionType_None
	//		TagType_PhotometricInterpretation(DataType_Short): TagValue_PhotometricType_WhiteIsZero
	//		TagType_DocumentName(DataType_ASCII): /Users/bsiegert/Developer/Go/src/pkg/image/tiff/testdata/bw-deflate.tiff
	//		TagType_StripOffsets(DataType_Long): [0x00000008]
	//		TagType_Orientation(DataType_Short): 1
	//		TagType_SamplesPerPixel(DataType_Short): 1
	//		TagType_RowsPerStrip(DataType_Short): 55
	//		TagType_StripByteCounts(DataType_Long): [1100]
	//		TagType_XResolution(DataType_Rational): [1207959552 16777216]
	//		TagType_YResolution(DataType_Rational): [1207959552 16777216]
	//		TagType_PlanarConfiguration(DataType_Short): 1
	//		TagType_ResolutionUnit(DataType_Short): TagValue_ResolutionUnitType_PerInch
	//		Next: 0x00000000
	//	}
	//
	// The TagType_ImageWidth(256) tag offset is (0x00000454+12*1+2).
	// We replace the ASCII dataype by invalid value large than len(lengths),
	// so that will cause panic in decoder.ifdUint.

	const (
		tagVal = 256
		tagOff = 0x00000454 + 12*1 + 2
	)

	if v := binary.LittleEndian.Uint16(b[tagOff : tagOff+2]); v != tagVal {
		t.Fatal(`could not find ImageWidth tag`, v)
	}
	binary.LittleEndian.PutUint16(b[tagOff+2:], uint16(len(lengths)+3)) // invalid datatype
	binary.LittleEndian.PutUint32(b[tagOff+4:], uint32(1))              // Count = 1

	if _, err = Decode(bytes.NewReader(b)); err == nil {
		t.Fatal("got nil error, want non-nil")
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment