Skip to content

Commit d07dae8

Browse files
Ignore exceptions trying to read IPTC-IIM data
1 parent b2b2110 commit d07dae8

File tree

3 files changed

+110
-4
lines changed

3 files changed

+110
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using NUnit.Framework;
2+
using NUnit.Framework;
3+
using TagLib;
4+
using TagLib.IFD;
5+
using TagLib.IFD.Tags;
6+
using TagLib.IFD.Entries;
7+
using TagLib.Jpeg;
8+
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Text;
13+
using System.Threading.Tasks;
14+
using TaglibSharp.Tests.Images.Validators;
15+
using TagLib.IFD;
16+
using TagLib.Xmp;
17+
18+
namespace TaglibSharp.Tests.Images
19+
{
20+
public class BadIptcSectionTest
21+
{
22+
[Test]
23+
public void Test ()
24+
{
25+
ImageTest.Run ("sample_IPTC_crash.jpg",
26+
false,
27+
new BadIptcSectionTestInvariantValidator (),
28+
NoModificationValidator.Instance,
29+
new PropertyModificationValidator<string> ("Copyright", null, "Copyright 2024 by Somebody Im Sure")
30+
);
31+
}
32+
}
33+
public class BadIptcSectionTestInvariantValidator : IMetadataInvariantValidator
34+
{
35+
int calls = 0;
36+
public void ValidateMetadataInvariants (TagLib.Image.File file)
37+
{
38+
// If we get here, the fix works.
39+
++calls;
40+
41+
Assert.IsNotNull (file);
42+
43+
Assert.IsFalse (file.PossiblyCorrupt); // The only problem is in the IPTC section, which we ignore.
44+
45+
var tag = file.GetTag (TagTypes.TiffIFD) as IFDTag;
46+
Assert.IsNotNull (tag, "IFD tag not found");
47+
48+
var structure = tag.Structure;
49+
50+
var exif = structure.GetEntry (0, (ushort)IFDEntryTag.ExifIFD) as SubIFDEntry;
51+
Assert.IsNotNull (exif, "Exif tag not found");
52+
53+
var exif_structure = exif.Structure;
54+
{
55+
var entry = exif_structure.GetEntry (0, (ushort)ExifEntryTag.ExifVersion);
56+
Assert.IsNotNull (entry, "Entry 0x9000 missing in IFD 0");
57+
Assert.IsNotNull (entry as UndefinedIFDEntry, "Entry is not an undefined IFD entry!");
58+
var parsed_bytes = (entry as UndefinedIFDEntry).Data.Data;
59+
var bytes = new byte[] { 48, 50, 51, 49 };
60+
Assert.AreEqual (bytes, parsed_bytes);
61+
}
62+
{
63+
var entry = exif_structure.GetEntry (0, (ushort)ExifEntryTag.ColorSpace);
64+
Assert.IsNotNull (entry, "Entry 0xa001 missing in IFD 0");
65+
Assert.IsNotNull (entry as ShortIFDEntry, "Entry is not a short IFD entry!");
66+
var parsed_value = (entry as ShortIFDEntry).Value;
67+
Assert.AreEqual (65535, parsed_value);
68+
}
69+
{
70+
var entry = exif_structure.GetEntry (0, (ushort)ExifEntryTag.PixelXDimension);
71+
Assert.IsNotNull (entry, "Entry 0xa001 missing in IFD 0");
72+
Assert.IsNotNull (entry as LongIFDEntry, "Entry is not a long IFD entry!");
73+
var parsed_value = (entry as LongIFDEntry).Value;
74+
Assert.AreEqual (4845, parsed_value);
75+
}
76+
{
77+
var entry = exif_structure.GetEntry (0, (ushort)ExifEntryTag.PixelYDimension);
78+
Assert.IsNotNull (entry, "Entry 0xa001 missing in IFD 0");
79+
Assert.IsNotNull (entry as LongIFDEntry, "Entry is not a long IFD entry!");
80+
var parsed_value = (entry as LongIFDEntry).Value;
81+
Assert.AreEqual (2834, parsed_value);
82+
}
83+
84+
85+
var xmp = file.GetTag (TagTypes.XMP) as XmpTag;
86+
Assert.IsNotNull (xmp, "XMP tag not found");
87+
88+
Assert.AreEqual ("Adobe Photoshop 22.1 (Windows)", xmp.Software);
89+
// ValidateMetadataInvariants is called 3 times for each Validator: once in the
90+
// ParseUnmodifiedFile method, once in the ModifyFile method before changing
91+
// anything, and once in the ParseModifiedFile method.
92+
// The PropertyModificationValidator class verifies the property setting, but
93+
// I'm not sure I totally trust it, so I'm going to check the property value
94+
// here as well.
95+
if (calls == 1)
96+
Assert.IsNull (xmp.Copyright);
97+
if (xmp.Copyright != null)
98+
Assert.AreEqual ("Copyright 2024 by Somebody Im Sure", xmp.Copyright);
99+
Assert.IsNull (xmp.Creator);
100+
}
101+
}
102+
}
Loading

src/TaglibSharp/Jpeg/File.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,14 @@ void ReadAPP13Segment (ushort length)
561561
}
562562
data.RemoveRange (0, iptc_iim_length + lenToSkip);
563563

564-
var reader = new IIM.IIMReader (data);
565-
var tag = reader.Process ();
566-
if (tag != null)
567-
ImageTag.AddTag (tag);
564+
try {
565+
var reader = new IIM.IIMReader (data);
566+
var tag = reader.Process ();
567+
if (tag != null)
568+
ImageTag.AddTag (tag);
569+
} catch (Exception) {
570+
// There isn't much we handle in the IPTC section, so we just ignore any errors.
571+
}
568572
}
569573

570574
/// <summary>

0 commit comments

Comments
 (0)