Skip to content

Commit

Permalink
exif: Fix handling of utf8 runes in nullString()
Browse files Browse the repository at this point in the history
  • Loading branch information
moorereason authored and bep committed Mar 13, 2021
1 parent 0a2ab3f commit f6612d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
15 changes: 6 additions & 9 deletions resources/images/exif/exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions resources/images/exif/exif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit f6612d8

Please # to comment.