Skip to content

Commit 80af170

Browse files
committed
Add threshold for stackalloc when reading descriptor
1 parent 8db6283 commit 80af170

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/native/managed/cdacreader/src/Target.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public struct TargetPointer
1616

1717
internal sealed unsafe class Target
1818
{
19+
private const int StackAllocByteThreshold = 1024;
1920
private static readonly ReadOnlyMemory<byte> MagicLE = new byte[] { 0x44, 0x4e, 0x43, 0x43, 0x44, 0x41, 0x43, 0x00 }; // "DNCCDAC\0"
2021
private static readonly ReadOnlyMemory<byte> MagicBE = new byte[] { 0x00, 0x43, 0x41, 0x44, 0x43, 0x43, 0x4e, 0x44 };
2122

@@ -75,7 +76,9 @@ private void ReadContractDescriptor(ulong address)
7576

7677
// Read descriptor
7778
// TODO: [cdac] Pass to JSON parser
78-
Span<byte> descriptorBuffer = stackalloc byte[(int)descriptorSize];
79+
Span<byte> descriptorBuffer = descriptorSize <= StackAllocByteThreshold
80+
? stackalloc byte[(int)descriptorSize]
81+
: new byte[(int)descriptorSize];
7982
if (ReadFromTarget(descriptor.Value, descriptorBuffer) < 0)
8083
throw new InvalidOperationException("Failed to read descriptor.");
8184

0 commit comments

Comments
 (0)