adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.28k stars 480 forks source link

Checking if a 7z file is encrypted will crash #761

Open njlr opened 1 year ago

njlr commented 1 year ago

Create a 7z file:

$ 7zz a test.7z

Check the entries are encrypted:

Scratch.fsx

#r "nuget: SharpCompress, 0.33.0"

open SharpCompress
open SharpCompress.Archives

let archive = ArchiveFactory.Open("test.7z")

for e in archive.Entries do
  printfn $"IsEncrypted %b{e.IsEncrypted}"
$ dotnet fsi ./Scratch.fsx

It will crash:

System.NullReferenceException: Object reference not set to an instance of an object.
   at SharpCompress.Common.SevenZip.SevenZipFilePart.get_IsEncrypted()
   at SharpCompress.Common.SevenZip.SevenZipEntry.get_IsEncrypted()
Erior commented 1 year ago

To see if I recreated the issue correctly, if you check if it is a directory before checking the IsEncrypted flag if not e.IsDirectory then Does that do anything for you?

njlr commented 1 year ago

This does not crash:

#r "nuget: SharpCompress, 0.33.0"

open SharpCompress
open SharpCompress.Archives

let archive = ArchiveFactory.Open("test.7z")

for e in archive.Entries do
  if e.IsDirectory then
    printfn $"%A{e} is a directory"
  else
    printfn $"IsEncrypted %b{e.IsEncrypted}"

Perhaps the error message should be improved?

Can a directory be encrypted?

adamhathcock commented 1 year ago

This was fixed: https://github.com/adamhathcock/sharpcompress/pull/768