Skip to content

Commit 82803d7

Browse files
committed
fix: add missing file read validation checks in assembly loader
Signed-off-by: jbrinkman <[email protected]>
1 parent 665e876 commit 82803d7

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/DotNetApiDiff/AssemblyLoading/AssemblyLoader.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ private bool IsProbablyNativeDll(string filePath)
285285
{
286286
return false; // Not enough bytes to determine if it's a DLL
287287
}
288+
288289
int peOffset = BitConverter.ToInt32(offsetBuffer, 0);
289290

290291
// Seek to the PE header
@@ -311,7 +312,12 @@ private bool IsProbablyNativeDll(string filePath)
311312

312313
// Read the Optional Header magic value
313314
byte[] magicBuffer = new byte[2];
314-
fileStream.Read(magicBuffer, 0, 2);
315+
bytesRead = 0;
316+
bytesRead = fileStream.Read(magicBuffer, 0, 2);
317+
if (bytesRead < 2)
318+
{
319+
return false; // Not enough bytes to determine if it's a DLL
320+
}
315321

316322
// PE32 (0x10B) or PE32+ (0x20B)
317323
ushort magic = BitConverter.ToUInt16(magicBuffer, 0);
@@ -338,7 +344,13 @@ private bool IsProbablyNativeDll(string filePath)
338344

339345
// Read the CLR header RVA and size
340346
byte[] clrBuffer = new byte[8];
341-
fileStream.Read(clrBuffer, 0, 8);
347+
bytesRead = 0;
348+
bytesRead = fileStream.Read(clrBuffer, 0, 8);
349+
if (bytesRead < 8)
350+
{
351+
return false; // Not enough bytes to determine if it's a DLL
352+
}
353+
342354
uint clrRva = BitConverter.ToUInt32(clrBuffer, 0);
343355
uint clrSize = BitConverter.ToUInt32(clrBuffer, 4);
344356

0 commit comments

Comments
 (0)