Skip to content

Commit 19a6f5f

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

File tree

3 files changed

+101
-4
lines changed

3 files changed

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

src/TaglibSharp/Jpeg/File.cs

Lines changed: 8 additions & 4 deletions
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)