From e5af635bd94fb1540604aa40b58b89ce0d1ee4a9 Mon Sep 17 00:00:00 2001 From: bd_ Date: Sat, 17 Aug 2024 18:44:12 -0700 Subject: [PATCH] fix: memory corruption issue in NativeMemoryManager (#1013) --- Runtime/ArmatureAwase/NativeMemoryManager.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Runtime/ArmatureAwase/NativeMemoryManager.cs b/Runtime/ArmatureAwase/NativeMemoryManager.cs index eaf6ae43..0f98b920 100644 --- a/Runtime/ArmatureAwase/NativeMemoryManager.cs +++ b/Runtime/ArmatureAwase/NativeMemoryManager.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; +using UnityEngine; #endregion @@ -134,6 +135,15 @@ namespace nadena.dev.modular_avatar.core.armature_lock void SetInUseMask(int offset, int length, bool value) { + if (offset < 0) + { + throw new ArgumentOutOfRangeException(); + } + + // We perform trial creations of segments (and then immediately free them if they exceed the bounds of the + // array). As such, we clamp the length, rather than throwing an exception. + length = Math.Min(length, InUseMask.Array.Length - offset); + unsafe { UnsafeUtility.MemSet((byte*)InUseMask.Array.GetUnsafePtr() + offset, value ? (byte)1 : (byte)0, length);