Skip to content

Commit

Permalink
Ensure stack allocations are gaurded
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcarbon committed Feb 19, 2024
1 parent 1cb62e7 commit dd06d8a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions MetadataExtractor/Formats/Apple/BplistReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static PropertyListResults Parse(ReadOnlySpan<byte> bplist)
{
for (int i = 0; i < offsets.Length; i++)
{
offsets[i] = BinaryPrimitives.ReadUInt16BigEndian(offsetsBytes.Slice(i * 2, 2));
offsets[i] = BinaryPrimitives.ReadInt16BigEndian(offsetsBytes.Slice(i * 2, 2));
}
}

Expand Down Expand Up @@ -133,7 +133,7 @@ static Dictionary<byte, byte> HandleDict(ref BufferReader reader, byte count)
return map;
}

static object HandleData(ref BufferReader reader, byte marker)
object HandleData(ref BufferReader reader, byte marker)
{
int byteCount = marker;

Expand Down
23 changes: 15 additions & 8 deletions MetadataExtractor/Formats/Bmp/BmpReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,22 @@ private static void ReadBitmapHeader(SequentialReader reader, BmpHeaderDirectory
}
else
{
using var iccBuffer = profileSize <= 256
? new BufferScope(stackalloc byte[profileSize])
: new BufferScope(profileSize);
if (profileSize <= 0)
{
directory.AddError($"Invalid ICC profile size. Was {profileSize}");
}
else
{
using var iccBuffer = profileSize <= 256
? new BufferScope(stackalloc byte[profileSize])
: new BufferScope(profileSize);

reader.GetBytes(iccBuffer.Span);
reader.GetBytes(iccBuffer.Span);

var iccDirectory = new IccReader().Extract(iccBuffer.Span);
iccDirectory.Parent = directory;
directories.Add(iccDirectory);
var iccDirectory = new IccReader().Extract(iccBuffer.Span);
iccDirectory.Parent = directory;
directories.Add(iccDirectory);
}
}
}
else
Expand All @@ -369,7 +376,7 @@ private static void ReadBitmapHeader(SequentialReader reader, BmpHeaderDirectory
}
else
{
directory.AddError("Unexpected DIB header size: " + headerSize);
directory.AddError($"Unexpected DIB header size: {headerSize}");
}
}
catch (IOException)
Expand Down
3 changes: 3 additions & 0 deletions MetadataExtractor/IO/BufferReader.Indexed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ public readonly double GetDouble64(int index)

public readonly string GetString(int index, int bytesRequested, Encoding encoding)
{
if (bytesRequested < 0)
throw new ArgumentOutOfRangeException(nameof(bytesRequested), "Must be 0 or greater");

// This check is important on .NET Framework
if (bytesRequested is 0)
{
Expand Down
13 changes: 8 additions & 5 deletions MetadataExtractor/IO/BufferReader.Sequential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,20 @@ public ulong GetUInt64()

public string GetString(int bytesRequested, Encoding encoding)
{
if (bytesRequested < 0)
throw new ArgumentOutOfRangeException(nameof(bytesRequested), "Must be 0 or greater");

// This check is important on .NET Framework
if (bytesRequested is 0)
return "";

Span<byte> bytes = bytesRequested <= 256
? stackalloc byte[bytesRequested]
: new byte[bytesRequested];
using BufferScope bytes = bytesRequested <= 256
? new BufferScope(stackalloc byte[bytesRequested])
: new BufferScope(bytesRequested);

GetBytes(bytes);
GetBytes(bytes.Span);

return encoding.GetString(bytes);
return encoding.GetString(bytes.Span);
}

public StringValue GetNullTerminatedStringValue(int maxLengthBytes, Encoding? encoding = null, bool moveToMaxLength = false)
Expand Down
3 changes: 3 additions & 0 deletions MetadataExtractor/IO/IndexedReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ public double GetDouble64(int index)
/// <exception cref="IOException"/>
public string GetString(int index, int bytesRequested, Encoding encoding)
{
if (bytesRequested < 0)
throw new ArgumentOutOfRangeException(nameof(bytesRequested), "Must be 0 or greater");

// This check is important on .NET Framework
if (bytesRequested is 0)
{
Expand Down

0 comments on commit dd06d8a

Please sign in to comment.