From ccc8d209da4ac2d2591fd9fe68ec64681671f9f1 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 26 Mar 2026 15:33:14 +0100 Subject: [PATCH] Fix `MeshAccessor` to validate buffer existence in C# API --- Source/Engine/Graphics/Models/MeshAccessor.cs | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/Source/Engine/Graphics/Models/MeshAccessor.cs b/Source/Engine/Graphics/Models/MeshAccessor.cs index ffe509476..1db33b5ba 100644 --- a/Source/Engine/Graphics/Models/MeshAccessor.cs +++ b/Source/Engine/Graphics/Models/MeshAccessor.cs @@ -76,6 +76,10 @@ namespace FlaxEngine /// Loaded value. public int GetInt(int index) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif fixed (byte* data = _data) return (int)_sampler.Read(data + index * _stride).X; } @@ -87,6 +91,10 @@ namespace FlaxEngine /// Loaded value. public float GetFloat(int index) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif fixed (byte* data = _data) return _sampler.Read(data + index * _stride).X; } @@ -98,6 +106,10 @@ namespace FlaxEngine /// Loaded value. public Float2 GetFloat2(int index) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif fixed (byte* data = _data) return new Float2(_sampler.Read(data + index * _stride)); } @@ -109,6 +121,10 @@ namespace FlaxEngine /// Loaded value. public Float3 GetFloat3(int index) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif fixed (byte* data = _data) return new Float3(_sampler.Read(data + index * _stride)); } @@ -120,6 +136,10 @@ namespace FlaxEngine /// Loaded value. public Float4 GetFloat4(int index) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif fixed (byte* data = _data) return _sampler.Read(data + index * _stride); } @@ -131,6 +151,10 @@ namespace FlaxEngine /// Value to assign. public void SetInt(int index, int value) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif var v = new Float4(value); fixed (byte* data = _data) _sampler.Write(data + index * _stride, ref v); @@ -143,6 +167,10 @@ namespace FlaxEngine /// Value to assign. public void SetFloat(int index, float value) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif var v = new Float4(value); fixed (byte* data = _data) _sampler.Write(data + index * _stride, ref v); @@ -155,6 +183,10 @@ namespace FlaxEngine /// Value to assign. public void SetFloat2(int index, Float2 value) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif var v = new Float4(value, 0.0f, 0.0f); fixed (byte* data = _data) _sampler.Write(data + index * _stride, ref v); @@ -167,6 +199,10 @@ namespace FlaxEngine /// Value to assign. public void SetFloat3(int index, Float3 value) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif var v = new Float4(value, 0.0f); fixed (byte* data = _data) _sampler.Write(data + index * _stride, ref v); @@ -179,6 +215,10 @@ namespace FlaxEngine /// Value to assign. public void SetFloat4(int index, Float4 value) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif fixed (byte* data = _data) _sampler.Write(data + index * _stride, ref value); } @@ -190,6 +230,10 @@ namespace FlaxEngine /// Pointer to the source data. public void SetLinear(IntPtr data) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif new Span(data.ToPointer(), _data.Length).CopyTo(_data); } @@ -199,6 +243,10 @@ namespace FlaxEngine /// The source . public void Set(Span src) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif if (IsLinear(PixelFormat.R32G32_Float)) { src.CopyTo(MemoryMarshal.Cast(_data)); @@ -223,6 +271,10 @@ namespace FlaxEngine /// The source . public void Set(Span src) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif if (IsLinear(PixelFormat.R32G32B32_Float)) { src.CopyTo(MemoryMarshal.Cast(_data)); @@ -247,6 +299,10 @@ namespace FlaxEngine /// The source . public void Set(Span src) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif if (IsLinear(PixelFormat.R32G32B32A32_Float)) { src.CopyTo(MemoryMarshal.Cast(_data)); @@ -271,6 +327,10 @@ namespace FlaxEngine /// The source . public void Set(Span src) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif if (IsLinear(PixelFormat.R32_UInt)) { src.CopyTo(MemoryMarshal.Cast(_data)); @@ -304,6 +364,10 @@ namespace FlaxEngine /// The destination . public void CopyTo(Span dst) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif if (IsLinear(PixelFormat.R32G32_Float)) { _data.CopyTo(MemoryMarshal.Cast(dst)); @@ -325,6 +389,10 @@ namespace FlaxEngine /// The destination . public void CopyTo(Span dst) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif if (IsLinear(PixelFormat.R32G32B32_Float)) { _data.CopyTo(MemoryMarshal.Cast(dst)); @@ -346,6 +414,10 @@ namespace FlaxEngine /// The destination . public void CopyTo(Span dst) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif if (IsLinear(PixelFormat.R32G32B32A32_Float)) { _data.CopyTo(MemoryMarshal.Cast(dst)); @@ -367,6 +439,10 @@ namespace FlaxEngine /// The destination . public void CopyTo(Span dst) { +#if !BUILD_RELEASE + if (_data == null) + throw new NullReferenceException("Missing stream data. Ensure to allocate mesh buffer or check for its existence."); +#endif if (IsLinear(PixelFormat.R32_UInt)) { _data.CopyTo(MemoryMarshal.Cast(dst));