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 <caleb.cornett@outlook.com>
Reviewed-on: MoonsideGames/MoonWorks#33
Co-authored-by: TheSpydog <thespydog@noreply.example.org>
Co-committed-by: TheSpydog <thespydog@noreply.example.org>
moar-validation
TheSpydog 2022-11-12 03:42:35 +00:00 committed by cosmonaut
parent 934f3fd623
commit 5533eeb2fd
1 changed files with 2 additions and 4 deletions

View File

@ -784,16 +784,14 @@ namespace MoonWorks.Graphics
var elementSize = sizeof(T); var elementSize = sizeof(T);
fixed (T* ptr = &data[0]) fixed (T* ptr = &data[startElement])
{ {
var dataPtr = ptr + (startElement * elementSize);
Refresh.Refresh_SetBufferData( Refresh.Refresh_SetBufferData(
Device.Handle, Device.Handle,
Handle, Handle,
buffer.Handle, buffer.Handle,
bufferOffsetInBytes, bufferOffsetInBytes,
(IntPtr) dataPtr, (IntPtr) ptr,
(uint) (numElements * elementSize) (uint) (numElements * elementSize)
); );
} }