diff --git a/MapleLib/WzLib/WzProperties/WzBinaryProperty.cs b/MapleLib/WzLib/WzProperties/WzBinaryProperty.cs index 5ab6d0a..04b5a87 100644 --- a/MapleLib/WzLib/WzProperties/WzBinaryProperty.cs +++ b/MapleLib/WzLib/WzProperties/WzBinaryProperty.cs @@ -326,14 +326,21 @@ private static byte[] StructToBytes(T obj) } } - private static T BytesToStructConstructorless(byte[] data) + private static T BytesToStructConstructorless(byte[] data) where T : class { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + int objectSize = Marshal.SizeOf(typeof(T)); + if (data.Length < objectSize) + throw new ArgumentException($"The byte array must be at least {objectSize} bytes long to contain the entire object.", nameof(data)); + GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); try { - T obj = (T)FormatterServices.GetUninitializedObject(typeof(T)); - Marshal.PtrToStructure(handle.AddrOfPinnedObject(), obj); - return obj; + IntPtr ptr = handle.AddrOfPinnedObject(); + object obj = Marshal.PtrToStructure(ptr, typeof(T)); + return (T)obj; } finally {