Skip to content

Commit cb4c4cb

Browse files
committed
Make exception handling less broad
1 parent 8008705 commit cb4c4cb

File tree

1 file changed

+85
-66
lines changed

1 file changed

+85
-66
lines changed

BinaryObjectScanner/Scanner.cs

Lines changed: 85 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,26 @@ private ProtectionDictionary GetInternalProtections(string file, int depth)
251251
using FileStream fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
252252
return GetInternalProtections(fs.Name, fs, depth);
253253
}
254+
catch (DirectoryNotFoundException ex)
255+
{
256+
if (_includeDebug) Console.WriteLine(ex);
257+
258+
return [];
259+
}
260+
catch (FileNotFoundException ex)
261+
{
262+
if (_includeDebug) Console.WriteLine(ex);
263+
264+
return [];
265+
}
266+
catch (UnauthorizedAccessException ex)
267+
{
268+
if (_includeDebug) Console.WriteLine(ex);
269+
270+
var protections = new ProtectionDictionary();
271+
protections.Append(file, _includeDebug ? ex.ToString() : "[Access issue when opening file, please check permissions and try again]");
272+
return protections;
273+
}
254274
catch (Exception ex)
255275
{
256276
if (_includeDebug) Console.WriteLine(ex);
@@ -283,98 +303,97 @@ private ProtectionDictionary GetInternalProtections(string fileName, Stream stre
283303
// Get the extension for certain checks
284304
string extension = Path.GetExtension(fileName).ToLower().TrimStart('.');
285305

286-
// Open the file and begin scanning
306+
// Get the first 16 bytes for matching
307+
byte[] magic;
287308
try
288309
{
289-
// Get the first 16 bytes for matching
290-
byte[] magic;
291-
try
292-
{
293-
int bytesToRead = (int)Math.Min(16, stream.Length);
294-
magic = stream.ReadBytes(bytesToRead);
295-
stream.Seek(0, SeekOrigin.Begin);
296-
}
297-
catch (Exception ex)
298-
{
299-
if (_includeDebug) Console.Error.WriteLine(ex);
300-
return [];
301-
}
310+
int bytesToRead = (int)Math.Min(16, stream.Length);
311+
magic = stream.ReadBytes(bytesToRead);
312+
stream.Seek(0, SeekOrigin.Begin);
313+
}
314+
catch (Exception ex)
315+
{
316+
if (_includeDebug) Console.Error.WriteLine(ex);
317+
return [];
318+
}
302319

303-
// Get the file type either from magic number or extension
304-
WrapperType fileType = WrapperFactory.GetFileType(magic, extension);
305-
if (fileType == WrapperType.UNKNOWN)
306-
{
307-
if (_includeDebug) Console.WriteLine($"{fileName} not a scannable file type, skipping...");
308-
return [];
309-
}
320+
// Get the file type either from magic number or extension
321+
WrapperType fileType = WrapperFactory.GetFileType(magic, extension);
322+
if (fileType == WrapperType.UNKNOWN)
323+
{
324+
if (_includeDebug) Console.WriteLine($"{fileName} not a scannable file type, skipping...");
325+
return [];
326+
}
310327

311-
// Get the wrapper, if possible
312-
var wrapper = WrapperFactory.CreateWrapper(fileType, stream);
328+
// Get the wrapper, if possible
329+
var wrapper = WrapperFactory.CreateWrapper(fileType, stream);
313330

314-
#region Non-Archive File Types
331+
#region Non-Archive File Types
315332

316-
// Try to scan file contents
317-
var detectable = CreateDetectable(fileType, wrapper);
318-
if (_scanContents && detectable != null)
333+
// Try to scan file contents
334+
var detectable = CreateDetectable(fileType, wrapper);
335+
if (_scanContents && detectable != null)
336+
{
337+
try
319338
{
320339
var subProtection = detectable.Detect(stream, fileName, _includeDebug);
321340
protections.Append(fileName, subProtection);
322341
}
342+
catch (Exception ex)
343+
{
344+
if (_includeDebug) Console.WriteLine(ex);
345+
protections.Append(fileName, _includeDebug ? ex.ToString() : "[Exception opening file, please try again]");
346+
}
347+
}
323348

324-
#endregion
349+
#endregion
325350

326-
#region Archive File Types
351+
#region Archive File Types
327352

328-
// If we're scanning archives
329-
if (_scanArchives && wrapper is IExtractable extractable)
353+
// If we're scanning archives
354+
if (_scanArchives && wrapper is IExtractable extractable)
355+
{
356+
// If the extractable file itself fails
357+
try
330358
{
331-
// If the extractable file itself fails
332-
try
333-
{
334-
// Extract and get the output path
335-
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
336-
Directory.CreateDirectory(tempPath);
337-
_ = extractable.Extract(tempPath, _includeDebug);
359+
// Extract and get the output path
360+
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
361+
Directory.CreateDirectory(tempPath);
362+
_ = extractable.Extract(tempPath, _includeDebug);
338363

339-
// Check if any files extracted
340-
if (IOExtensions.SafeGetFiles(tempPath).Length > 0)
341-
{
342-
// Scan the output path
343-
var subProtections = GetProtectionsImpl(tempPath, depth + 1);
364+
// Check if any files extracted
365+
if (IOExtensions.SafeGetFiles(tempPath).Length > 0)
366+
{
367+
// Scan the output path
368+
var subProtections = GetProtectionsImpl(tempPath, depth + 1);
344369

345-
// Prepare the returned values
346-
subProtections.StripFromKeys(tempPath);
347-
subProtections.PrependToKeys(fileName);
370+
// Prepare the returned values
371+
subProtections.StripFromKeys(tempPath);
372+
subProtections.PrependToKeys(fileName);
348373

349-
// Append the values
350-
protections.Append(subProtections);
351-
}
374+
// Append the values
375+
protections.Append(subProtections);
376+
}
352377

353-
// If temp directory cleanup fails
354-
try
355-
{
356-
if (Directory.Exists(tempPath))
357-
Directory.Delete(tempPath, true);
358-
}
359-
catch (Exception ex)
360-
{
361-
if (_includeDebug) Console.Error.WriteLine(ex);
362-
}
378+
// If temp directory cleanup fails
379+
try
380+
{
381+
if (Directory.Exists(tempPath))
382+
Directory.Delete(tempPath, true);
363383
}
364384
catch (Exception ex)
365385
{
366386
if (_includeDebug) Console.Error.WriteLine(ex);
367387
}
368388
}
369-
370-
#endregion
371-
}
372-
catch (Exception ex)
373-
{
374-
if (_includeDebug) Console.WriteLine(ex);
375-
protections.Append(fileName, _includeDebug ? ex.ToString() : "[Exception opening file, please try again]");
389+
catch (Exception ex)
390+
{
391+
if (_includeDebug) Console.Error.WriteLine(ex);
392+
}
376393
}
377394

395+
#endregion
396+
378397
// Clear out any empty keys
379398
protections.ClearEmptyKeys();
380399

0 commit comments

Comments
 (0)