Merge pull request #171 from greut/fix-170

Check the length before jumping into buffer.
master
Tomás Aparicio 9 years ago committed by GitHub
commit 14ff7ff2c8

@ -562,35 +562,36 @@ func vipsAffine(input *C.VipsImage, residualx, residualy float64, i Interpolator
} }
func vipsImageType(buf []byte) ImageType { func vipsImageType(buf []byte) ImageType {
if len(buf) == 0 { if len(buf) < 12 {
return UNKNOWN return UNKNOWN
} }
if buf[0] == 0x89 && buf[1] == 0x50 && buf[2] == 0x4E && buf[3] == 0x47 {
return PNG
}
if buf[0] == 0xFF && buf[1] == 0xD8 && buf[2] == 0xFF { if buf[0] == 0xFF && buf[1] == 0xD8 && buf[2] == 0xFF {
return JPEG return JPEG
} }
if IsTypeSupported(WEBP) && buf[8] == 0x57 && buf[9] == 0x45 && buf[10] == 0x42 && buf[11] == 0x50 { if IsTypeSupported(GIF) && buf[0] == 0x47 && buf[1] == 0x49 && buf[2] == 0x46 {
return WEBP return GIF
}
if buf[0] == 0x89 && buf[1] == 0x50 && buf[2] == 0x4E && buf[3] == 0x47 {
return PNG
} }
if IsTypeSupported(TIFF) && if IsTypeSupported(TIFF) &&
((buf[0] == 0x49 && buf[1] == 0x49 && buf[2] == 0x2A && buf[3] == 0x0) || ((buf[0] == 0x49 && buf[1] == 0x49 && buf[2] == 0x2A && buf[3] == 0x0) ||
(buf[0] == 0x4D && buf[1] == 0x4D && buf[2] == 0x0 && buf[3] == 0x2A)) { (buf[0] == 0x4D && buf[1] == 0x4D && buf[2] == 0x0 && buf[3] == 0x2A)) {
return TIFF return TIFF
} }
if IsTypeSupported(GIF) && buf[0] == 0x47 && buf[1] == 0x49 && buf[2] == 0x46 {
return GIF
}
if IsTypeSupported(PDF) && buf[0] == 0x25 && buf[1] == 0x50 && buf[2] == 0x44 && buf[3] == 0x46 { if IsTypeSupported(PDF) && buf[0] == 0x25 && buf[1] == 0x50 && buf[2] == 0x44 && buf[3] == 0x46 {
return PDF return PDF
} }
if IsTypeSupported(WEBP) && buf[8] == 0x57 && buf[9] == 0x45 && buf[10] == 0x42 && buf[11] == 0x50 {
return WEBP
}
if IsTypeSupported(SVG) && IsSVGImage(buf) { if IsTypeSupported(SVG) && IsSVGImage(buf) {
return SVG return SVG
} }
if IsTypeSupported(MAGICK) && strings.HasSuffix(readImageType(buf), "MagickBuffer") { if IsTypeSupported(MAGICK) && strings.HasSuffix(readImageType(buf), "MagickBuffer") {
return MAGICK return MAGICK
} }
return UNKNOWN return UNKNOWN
} }

@ -144,6 +144,13 @@ func TestVipsImageType(t *testing.T) {
} }
} }
func TestVipsImageTypeInvalid(t *testing.T) {
imgType := vipsImageType([]byte("vip"))
if imgType != UNKNOWN {
t.Fatal("Invalid image type")
}
}
func TestVipsMemory(t *testing.T) { func TestVipsMemory(t *testing.T) {
mem := VipsMemory() mem := VipsMemory()

Loading…
Cancel
Save