From e13122cab1997f4aa91cc30844d81694eaf352a6 Mon Sep 17 00:00:00 2001 From: Ildar Khayrutdinov Date: Sun, 19 Sep 2021 10:54:18 +0300 Subject: [PATCH] workaround for inconsistent covariance of value-typed arrays --- .../Profiles/Exif/Values/ExifLong8Array.cs | 37 +++++++++++++++---- .../Profiles/Exif/Values/ExifNumberArray.cs | 20 ++++++++-- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifLong8Array.cs b/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifLong8Array.cs index 055a501042..d012f646b5 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifLong8Array.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifLong8Array.cs @@ -51,25 +51,46 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif switch (value) { case int val: - return this.SetSingle((ulong)val); + return this.SetSingle((ulong)Numerics.Clamp(val, 0, int.MaxValue)); + case uint val: return this.SetSingle((ulong)val); + case short val: - return this.SetSingle((ulong)val); + return this.SetSingle((ulong)Numerics.Clamp(val, 0, short.MaxValue)); + case ushort val: return this.SetSingle((ulong)val); + case long[] array: + { + if (value.GetType().Equals(typeof(ulong[]))) + { + return this.SetArray((ulong[])value); + } + return this.SetArray(array); - case ulong[] array: - return this.SetArray(array); + } + case int[] array: + { + if (value.GetType().Equals(typeof(uint[]))) + { + return this.SetArray((uint[])value); + } + return this.SetArray(array); - case uint[] array: - return this.SetArray(array); + } + case short[] array: + { + if (value.GetType().Equals(typeof(ushort[]))) + { + return this.SetArray((ushort[])value); + } + return this.SetArray(array); - case ushort[] array: - return this.SetArray(array); + } } return false; diff --git a/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifNumberArray.cs b/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifNumberArray.cs index 2d3a93aed3..4b7433d7de 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifNumberArray.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifNumberArray.cs @@ -54,13 +54,25 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif case ushort val: return this.SetSingle(val); case int[] array: + { + // workaround for inconsistent covariance of value-typed arrays + if (value.GetType().Equals(typeof(uint[]))) + { + return this.SetArray((uint[])value); + } + return this.SetArray(array); - case uint[] array: - return this.SetArray(array); + } + case short[] array: + { + if (value.GetType().Equals(typeof(ushort[]))) + { + return this.SetArray((ushort[])value); + } + return this.SetArray(array); - case ushort[] array: - return this.SetArray(array); + } } return false;