From 5533eeb2fd146bc7f061082fb607698afbc160f7 Mon Sep 17 00:00:00 2001 From: TheSpydog Date: Sat, 12 Nov 2022 03:42:35 +0000 Subject: [PATCH] Fix bad pointer arithmetic in SetBufferData (#33) Fixes a bug where SetBufferData was writing garbage to the buffer if `startElement` was non-zero. Rather than fixing the pointer addition (it should have been `ptr + startElement`) I opted to remove it and instead pass the index explicitly when grabbing the address of the array element. I think that's a little easier to understand, and it's slightly safer too -- if you pass a startElement beyond the array bounds it will now throw an IndexOutOfRangeException instead of silently reading from outside the array bounds. Co-authored-by: Caleb Cornett Reviewed-on: https://gitea.moonside.games/MoonsideGames/MoonWorks/pulls/33 Co-authored-by: TheSpydog Co-committed-by: TheSpydog --- src/Graphics/CommandBuffer.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index 45a602e..923e766 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -784,16 +784,14 @@ namespace MoonWorks.Graphics var elementSize = sizeof(T); - fixed (T* ptr = &data[0]) + fixed (T* ptr = &data[startElement]) { - var dataPtr = ptr + (startElement * elementSize); - Refresh.Refresh_SetBufferData( Device.Handle, Handle, buffer.Handle, bufferOffsetInBytes, - (IntPtr) dataPtr, + (IntPtr) ptr, (uint) (numElements * elementSize) ); }