Skip to content

Commit 0dc3b37

Browse files
committed
Fix handling of converted undefined floats
Relates to the fix in 93e6518 See gohugoio/hugo#12793
1 parent 93e6518 commit 0dc3b37

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

‎imagemeta_test.go‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func TestDecodeWebP(t *testing.T) {
6969
c.Assert(tags.XMP()["City"].Value, qt.Equals, "Benalmádena")
7070
}
7171

72+
// For development.
73+
func TestDecodeAdhoc(t *testing.T) {
74+
t.Skip("used in development")
75+
extractTags(t, "/Users/bep/dump/summer.webp", imagemeta.EXIF)
76+
}
77+
7278
func TestDecodeJPEG(t *testing.T) {
7379
c := qt.New(t)
7480

@@ -615,15 +621,6 @@ func compareWithExiftoolOutput(t testing.TB, filename string, sources imagemeta.
615621
case imagemeta.Rat[int32]:
616622
return v.Float64()
617623
case float64:
618-
if math.IsInf(v, 1) {
619-
panic(fmt.Errorf("inf: %s", s))
620-
}
621-
if math.IsInf(v, -1) {
622-
panic(fmt.Errorf("-inf: %s", s))
623-
}
624-
if math.IsNaN(v) {
625-
panic(fmt.Errorf("nan: %s", s))
626-
}
627624
return v
628625
case int64:
629626
return float64(v)
@@ -710,6 +707,17 @@ func compareWithExiftoolOutput(t testing.TB, filename string, sources imagemeta.
710707
if found {
711708
expect := normalizeThem(v.Tag, exifToolValue, v.Source)
712709
got := normalizeUs(v.Tag, v.Value, v.Source)
710+
if v, ok := got.(float64); ok {
711+
if math.IsInf(v, 1) {
712+
panic(fmt.Errorf("inf: %v", v))
713+
}
714+
if math.IsInf(v, -1) {
715+
panic(fmt.Errorf("-inf: %v", v))
716+
}
717+
if math.IsNaN(v) {
718+
panic(fmt.Errorf("nan: %v", v))
719+
}
720+
}
713721
c.Assert(got, eq, expect, qt.Commentf("%s (%s): got: %T/%T %v %q\n\n%s\n\n%s", v.Tag, v.Source, got, expect, v.Value, filename, got, expect))
714722
}
715723
}
@@ -740,7 +748,10 @@ func extractTags(t testing.TB, filename string, sources imagemeta.Source) imagem
740748

741749
func extractTagsWithFilter(t testing.TB, filename string, sources imagemeta.Source, shouldHandle func(ti imagemeta.TagInfo) bool) imagemeta.Tags {
742750
t.Helper()
743-
f, err := os.Open(filepath.Join("testdata", "images", filename))
751+
if !filepath.IsAbs(filename) {
752+
filename = filepath.Join("testdata", "images", filename)
753+
}
754+
f, err := os.Open(filename)
744755
if err != nil {
745756
t.Fatal(err)
746757
}
@@ -775,6 +786,12 @@ func extractTagsWithFilter(t testing.TB, filename string, sources imagemeta.Sour
775786
// Verify that it can be marshaled to JSON.
776787
_, err = json.Marshal(tags.All())
777788
if err != nil {
789+
for tag, ti := range tags.All() {
790+
_, err2 := json.Marshal(ti.Value)
791+
if err2 != nil {
792+
t.Fatal(fmt.Errorf("failed to marshal tag %q in source %s with value %v/%T to JSON: %w", tag, ti.Source, ti.Value, ti.Value, err2))
793+
}
794+
}
778795
t.Fatal(fmt.Errorf("failed to marshal tags in %q to JSON: %w", filename, err))
779796
}
780797

‎metadecoder_exif.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func (e *metaDecoderEXIF) convertValue(typ exifType, r io.Reader) any {
157157
if isUndefined(float64(v)) {
158158
return undef
159159
}
160+
160161
}
161162

162163
return v
@@ -425,6 +426,9 @@ func (e *metaDecoderEXIF) decodeTag(namespace string) error {
425426
if convert, found := exifValueConverterMap[tagName]; found {
426427
e.valueConverterCtx.tagName = tagName
427428
val = convert(e.valueConverterCtx, val)
429+
if f, ok := val.(float64); ok && isUndefined(f) {
430+
val = undef
431+
}
428432
} else {
429433
val = toPrintableValue(val)
430434
}

0 commit comments

Comments
 (0)