exif: Fix handling of utf8 runes in nullString()

This commit is contained in:
Cameron Moore 2021-03-13 12:45:09 -06:00 committed by Bjørn Erik Pedersen
parent 0a2ab3f8fe
commit f6612d8bd8
2 changed files with 23 additions and 9 deletions

View file

@ -226,17 +226,14 @@ func (e *exifWalker) Walk(f _exif.FieldName, tag *tiff.Tag) error {
func nullString(in []byte) string {
var rv bytes.Buffer
for _, b := range in {
if unicode.IsGraphic(rune(b)) {
rv.WriteByte(b)
for len(in) > 0 {
r, size := utf8.DecodeRune(in)
if unicode.IsGraphic(r) {
rv.WriteRune(r)
}
in = in[size:]
}
rvs := rv.String()
if utf8.ValidString(rvs) {
return rvs
}
return ""
return rv.String()
}
var tcodec *tmc.Codec

View file

@ -89,6 +89,23 @@ func TestIssue8079(t *testing.T) {
c.Assert(x.Tags["ImageDescription"], qt.Equals, "Città del Vaticano #nanoblock #vatican #vaticancity")
}
func TestNullString(t *testing.T) {
c := qt.New(t)
for _, test := range []struct {
in string
expect string
}{
{"foo", "foo"},
{"\x20", "\x20"},
{"\xc4\x81", "\xc4\x81"}, // \u0101
{"\u0160", "\u0160"}, // non-breaking space
} {
res := nullString([]byte(test.in))
c.Assert(res, qt.Equals, test.expect)
}
}
func BenchmarkDecodeExif(b *testing.B) {
c := qt.New(b)
f, err := os.Open(filepath.FromSlash("../../testdata/sunset.jpg"))