Browse Source

Fixed paste empty bytes crash

Krzysztof Krysiński 2 years ago
parent
commit
564a38950f

+ 5 - 0
src/PixiEditor.DrawingApi.Core/Numerics/VecI.cs

@@ -86,6 +86,11 @@ public struct VecI : IEquatable<VecI>
 
 
     public static VecI FromBytes(ReadOnlySpan<byte> value)
     public static VecI FromBytes(ReadOnlySpan<byte> value)
     {
     {
+        if (value.Length < sizeof(int) * 2)
+        {
+            throw new ArgumentException("Value bytes are invalid. Span must have 8 bytes", nameof(value));
+        }
+
         var x = BitConverter.ToInt32(value);
         var x = BitConverter.ToInt32(value);
         var y = BitConverter.ToInt32(value[4..]);
         var y = BitConverter.ToInt32(value[4..]);
 
 

+ 12 - 1
src/PixiEditor/Helpers/ClipboardHelper.cs

@@ -43,7 +43,18 @@ internal static class ClipboardHelper
         }
         }
     }
     }
     
     
-    public static VecI GetVecI(this DataObject data, string format) => VecI.FromBytes((byte[])data.GetData(format));
+    public static VecI GetVecI(this DataObject data, string format)
+    {
+        if (!data.GetDataPresent(format))
+            return default;
+
+        byte[] bytes = (byte[])data.GetData(format);
+
+        if (bytes is { Length: < 8 })
+            return default;
+
+        return VecI.FromBytes(bytes);
+    }
 
 
     public static void SetVecI(this DataObject data, string format, VecI value) => data.SetData(format, value.ToByteArray());
     public static void SetVecI(this DataObject data, string format, VecI value) => data.SetData(format, value.ToByteArray());
 }
 }