From a2cd58f651a160f278f0ef4d712727f61d43ff77 Mon Sep 17 00:00:00 2001 From: Yaroslav Khrylchenko Date: Thu, 12 Nov 2020 17:00:34 +0300 Subject: [PATCH 1/2] fix panic on reading Exif --- vips.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vips.go b/vips.go index 7fa4606..266d065 100644 --- a/vips.go +++ b/vips.go @@ -228,8 +228,9 @@ func vipsExifOrientation(image *C.VipsImage) int { } func vipsExifShort(s string) string { - if strings.Contains(s, " (") { - return s[:strings.Index(s, "(")-1] + i := strings.Index(s, " (") + if i > 0 { + return s[:i] } return s } From 3b61e309828989a0a1560847ed798fba37d58c0e Mon Sep 17 00:00:00 2001 From: Yaroslav Khrylchenko Date: Thu, 12 Nov 2020 17:32:40 +0300 Subject: [PATCH 2/2] add test --- vips_test.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/vips_test.go b/vips_test.go index e3092bd..bf8d57d 100644 --- a/vips_test.go +++ b/vips_test.go @@ -89,8 +89,8 @@ func TestVipsAutoRotate(t *testing.T) { } files := []struct { - name string - orientation int + name string + orientation int }{ {"test.jpg", 0}, {"test_exif.jpg", 0}, @@ -203,6 +203,33 @@ func TestVipsMemory(t *testing.T) { } } +func TestVipsExifShort(t *testing.T) { + tt := []struct { + input string + expected string + }{ + { + input: `( ()`, + expected: `(`, + }, + { + input: ` ()`, + expected: ` ()`, + }, + { + input: `sRGB`, + expected: `sRGB`, + }, + } + + for _, tc := range tt { + got := vipsExifShort(tc.input) + if got != tc.expected { + t.Fatalf("expected: %s; got: %s", tc.expected, got) + } + } +} + func readImage(file string) []byte { img, _ := os.Open(path.Join("testdata", file)) buf, _ := ioutil.ReadAll(img)