From 80af1704557b43e1d015840338a2a007ac74b2fe Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Wed, 17 Apr 2024 15:43:03 -0700 Subject: [PATCH] Add threshold for stackalloc when reading descriptor --- src/native/managed/cdacreader/src/Target.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/native/managed/cdacreader/src/Target.cs b/src/native/managed/cdacreader/src/Target.cs index 3b4a88c8ce5ac..ccbcade35e1d6 100644 --- a/src/native/managed/cdacreader/src/Target.cs +++ b/src/native/managed/cdacreader/src/Target.cs @@ -16,6 +16,7 @@ public struct TargetPointer internal sealed unsafe class Target { + private const int StackAllocByteThreshold = 1024; private static readonly ReadOnlyMemory MagicLE = new byte[] { 0x44, 0x4e, 0x43, 0x43, 0x44, 0x41, 0x43, 0x00 }; // "DNCCDAC\0" private static readonly ReadOnlyMemory MagicBE = new byte[] { 0x00, 0x43, 0x41, 0x44, 0x43, 0x43, 0x4e, 0x44 }; @@ -75,7 +76,9 @@ private void ReadContractDescriptor(ulong address) // Read descriptor // TODO: [cdac] Pass to JSON parser - Span descriptorBuffer = stackalloc byte[(int)descriptorSize]; + Span descriptorBuffer = descriptorSize <= StackAllocByteThreshold + ? stackalloc byte[(int)descriptorSize] + : new byte[(int)descriptorSize]; if (ReadFromTarget(descriptor.Value, descriptorBuffer) < 0) throw new InvalidOperationException("Failed to read descriptor.");