Skip to content

Commit 44ef8f0

Browse files
committed
allocate uninitialized arrays when reading from disk or decoding
1 parent 40c6299 commit 44ef8f0

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

TACTLib/Container/ContainerHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private unsafe ArraySegment<byte> OpenIndexEntry(IndexEntry indexEntry)
181181
var dataHandle = m_dataFiles[indexEntry.Index];
182182

183183
var size = indexEntry.EncodedSize;
184-
var buffer = new byte[size];
184+
var buffer = GC.AllocateUninitializedArray<byte>((int)size);
185185
var bytesRead = RandomAccess.Read(dataHandle, buffer, indexEntry.Offset);
186186
if (bytesRead != buffer.Length)
187187
{

TACTLib/Container/StaticContainerHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private string GetFilePath(ulong chunk, ulong archive) {
8282

8383
using var stream = File.OpenRead(GetFilePath(chunk, archive));
8484
stream.Position = (long)offset;
85-
var data = new byte[eSize];
85+
var data = GC.AllocateUninitializedArray<byte>(eSize);
8686
stream.DefinitelyRead(data);
8787

8888
return data;

TACTLib/Core/BLTEDecoder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static unsafe byte[] Decode(ClientHandler? client, Span<byte> src) {
7777
{
7878
decodedSize += block.m_decodedSize;
7979
}
80-
var output = new byte[decodedSize];
80+
var output = GC.AllocateUninitializedArray<byte>(decodedSize);
8181
var outputSlice = output.AsSpan();
8282

8383
for (var i = 0; i < blocks.Length; i++)
@@ -87,6 +87,12 @@ public static unsafe byte[] Decode(ClientHandler? client, Span<byte> src) {
8787
var blockOutput = SpanHelper.Advance(ref outputSlice, block.m_decodedSize);
8888
HandleDataBlock(client, encodedBlockData, blockOutput, i);
8989
}
90+
91+
if (!outputSlice.IsEmpty)
92+
{
93+
// don't let uninitialized data leak back to caller
94+
throw new InvalidDataException("remaining data after decoding all blocks");
95+
}
9096
return output;
9197
}
9298

0 commit comments

Comments
 (0)