Skip to content

Commit

Permalink
Add threshold for stackalloc when reading descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung committed Apr 19, 2024
1 parent 8db6283 commit 80af170
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/native/managed/cdacreader/src/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct TargetPointer

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

Expand Down Expand Up @@ -75,7 +76,9 @@ private void ReadContractDescriptor(ulong address)

// Read descriptor
// TODO: [cdac] Pass to JSON parser
Span<byte> descriptorBuffer = stackalloc byte[(int)descriptorSize];
Span<byte> descriptorBuffer = descriptorSize <= StackAllocByteThreshold
? stackalloc byte[(int)descriptorSize]
: new byte[(int)descriptorSize];
if (ReadFromTarget(descriptor.Value, descriptorBuffer) < 0)
throw new InvalidOperationException("Failed to read descriptor.");

Expand Down

0 comments on commit 80af170

Please sign in to comment.