hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit f6612d8bd8c4c3bb498178d14f45d3acdf86aa7c
parent 0a2ab3f8feb961f8394b1f9964fab36bfa468027
Author: Cameron Moore <moorereason@gmail.com>
Date:   Sat, 13 Mar 2021 12:45:09 -0600

exif: Fix handling of utf8 runes in nullString()

Diffstat:
Mresources/images/exif/exif.go | 15++++++---------
Mresources/images/exif/exif_test.go | 17+++++++++++++++++
2 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/resources/images/exif/exif.go b/resources/images/exif/exif.go
@@ -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
diff --git a/resources/images/exif/exif_test.go b/resources/images/exif/exif_test.go
@@ -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"))