From 54df7ab641df05975e895036259d49f85703607b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 26 Jan 2023 15:23:35 +1000 Subject: [PATCH] Safer cast, cleanup and comment --- .../Metadata/Profiles/Exif/ExifProfile.cs | 18 +++++++++--------- .../Metadata/Profiles/Exif/ExifReader.cs | 1 + .../Metadata/Profiles/Exif/ExifWriter.cs | 5 ++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs index e14c88229..dd5792ae7 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs @@ -2,7 +2,6 @@ // Licensed under the Six Labors Split License. using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Metadata.Profiles.Exif; @@ -79,7 +78,7 @@ public sealed class ExifProfile : IDeepCloneable this.InvalidTags = other.InvalidTags.Count > 0 ? new List(other.InvalidTags) - : (IReadOnlyList)Array.Empty(); + : Array.Empty(); if (other.values != null) { @@ -161,7 +160,7 @@ public sealed class ExifProfile : IDeepCloneable return false; } - using (var memStream = new MemoryStream(this.data, this.thumbnailOffset, this.thumbnailLength)) + using (MemoryStream memStream = new(this.data, this.thumbnailOffset, this.thumbnailLength)) { image = Image.Load(memStream); return true; @@ -237,12 +236,12 @@ public sealed class ExifProfile : IDeepCloneable return Array.Empty(); } - var writer = new ExifWriter(this.values, this.Parts); + ExifWriter writer = new(this.values, this.Parts); return writer.GetData(); } /// - public ExifProfile DeepClone() => new ExifProfile(this); + public ExifProfile DeepClone() => new(this); /// /// Returns the value with the specified tag. @@ -267,6 +266,7 @@ public sealed class ExifProfile : IDeepCloneable /// /// The tag of the exif value. /// The value. + /// Newly created value is null. internal void SetValueInternal(ExifTag tag, object? value) { foreach (IExifValue exifValue in this.Values) @@ -281,7 +281,7 @@ public sealed class ExifProfile : IDeepCloneable ExifValue? newExifValue = ExifValues.Create(tag); if (newExifValue is null) { - throw new NotSupportedException(); + throw new NotSupportedException($"Newly created value for tag {tag} is null."); } newExifValue.TrySetValue(value); @@ -310,7 +310,7 @@ public sealed class ExifProfile : IDeepCloneable this.RemoveValue(value.Tag); } - var newResolution = new Rational(resolution, false); + Rational newResolution = new(resolution, false); this.SetValue(tag, newResolution); } @@ -328,13 +328,13 @@ public sealed class ExifProfile : IDeepCloneable return; } - var reader = new ExifReader(this.data); + ExifReader reader = new(this.data); this.values = reader.ReadValues(); this.InvalidTags = reader.InvalidTags.Count > 0 ? new List(reader.InvalidTags) - : (IReadOnlyList)Array.Empty(); + : Array.Empty(); this.thumbnailOffset = (int)reader.ThumbnailOffset; this.thumbnailLength = (int)reader.ThumbnailLength; diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs index 7b989c8d8..885db3a5e 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs @@ -22,6 +22,7 @@ internal class ExifReader : BaseExifReader public ExifReader(byte[] exifData, MemoryAllocator? allocator) : base(new MemoryStream(exifData ?? throw new ArgumentNullException(nameof(exifData))), allocator) { + // TODO: We never call this constructor passing a non-null allocator. } /// diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs index 76fb2ff39..1d2dca870 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs @@ -90,7 +90,7 @@ internal sealed class ExifWriter if (gpsLength > 0) { i = this.WriteHeaders(this.gpsValues, result, i); - i = this.WriteData(startIndex, this.gpsValues, result, i); + this.WriteData(startIndex, this.gpsValues, result, i); } return result; @@ -228,9 +228,8 @@ internal sealed class ExifWriter return false; } - if (exifValue.DataType == ExifDataType.Ascii) + if (exifValue.DataType == ExifDataType.Ascii && value is string stringValue) { - string stringValue = (string)value; return stringValue.Length > 0; }