Skip to content

Commit

Permalink
Merge pull request #1305 from sillsdev/BL-11933-HandleBadImageMetadata
Browse files Browse the repository at this point in the history
Catch ArgumentOutOfRangeException exception from TagLib
  • Loading branch information
hatton authored Nov 9, 2023
2 parents ff6e273 + a1b57e6 commit 5241079
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- [SIL.Windows.Forms.ClearShare] Fixed Metadata.LoadProperties to catch the ArgumentOutOfRangeException thrown by TagLib.File.Create when unknown data is found in the IPTC profile segment. The rest of the metadata (Exif / XMP) is likely to be okay, but won't be available until TagLib is fixed to allow this. Not having the metadata available shouldn't prevent using the image. Note that clients can now read the exception caught while loading if so desired.
- [SIL.Windows.Forms.WritingSystem.WSIdentifiers] Changed ComboBox controls in WSIdentifierView and ScriptRegionVariantView to DropDownList style to prevent accidental editing that shouldn't happen
- [SIL.Windows.Forms.ClearShare] Make Metadata.Write (and a few other methods) more robust
- [SIL.Core.Desktop] Make FileUtils.ReplaceFileWithUserInteractionIfNeeded robust
Expand Down
18 changes: 17 additions & 1 deletion SIL.Windows.Forms/ClearShare/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public bool IsOutOfMemoryPlausible(OutOfMemoryException ex)
return false;
}

public Exception ExceptionCaughtWhileLoading;

/// <summary>
/// NB: this is used in 2 places; one is loading from the image we are linked to, the other from a sample image we are copying metadata from
/// </summary>
Expand All @@ -110,16 +112,18 @@ private static void LoadProperties(string path, Metadata destinationMetadata)
{
try
{
destinationMetadata.ExceptionCaughtWhileLoading = null;
destinationMetadata._originalTaglibMetadata = RetryUtility.Retry(() =>
TagLib.File.Create(path) as TagLib.Image.File,
memo:$"LoadProperties({path})");
}
catch (TagLib.UnsupportedFormatException)
catch (TagLib.UnsupportedFormatException ex)
{
// TagLib throws this exception when the file doesn't have any metadata, sigh.
// So since I don't see a way to differentiate between that case and the case
// where something really is wrong, we're just gonna have to swallow this,
// even in DEBUG mode, because else a lot of simple image tests fail
destinationMetadata.ExceptionCaughtWhileLoading = ex;
return;
}
catch (NotImplementedException ex)
Expand All @@ -130,6 +134,18 @@ private static void LoadProperties(string path, Metadata destinationMetadata)
// problem, but have other limitations.
// See https://issues.bloomlibrary.org/youtrack/issue/BL-8706 for a user complaint.
System.Diagnostics.Debug.WriteLine($"TagLib exception: {ex}");
destinationMetadata.ExceptionCaughtWhileLoading = ex;
return;
}
catch (ArgumentOutOfRangeException ex)
{
// TagLib can throw this if it can't read some part of the metadata. This
// prevents us from even looking at images that have such metadata, which
// seems unreasonable. (TagLib doesn't fully understand IPTC profiles, for
// example, which can lead to this exception.)
// See https://issues.bloomlibrary.org/youtrack/issue/BL-11933 for a user complaint.
System.Diagnostics.Debug.WriteLine($"TagLib exception: {ex}");
destinationMetadata.ExceptionCaughtWhileLoading = ex;
return;
}
LoadProperties(destinationMetadata._originalTaglibMetadata.ImageTag, destinationMetadata);
Expand Down

0 comments on commit 5241079

Please sign in to comment.