Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont crash on reading rar5 comment #783 #784

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ public IEnumerable<IRarHeader> ReadHeaders(Stream stream)
case HeaderCodeV.RAR5_SERVICE_HEADER:
{
var fh = new FileHeader(header, reader, HeaderType.Service);
SkipData(fh, reader);
if (fh.FileName == "CMT")
{
fh.PackedStream = new ReadOnlySubStream(reader.BaseStream, fh.CompressedSize);
}
else
{
SkipData(fh, reader);
}
return fh;
}

Expand Down
7 changes: 2 additions & 5 deletions src/SharpCompress/Common/Rar/RarVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SharpCompress.Common.Rar.Headers;
using SharpCompress.IO;
using SharpCompress.Readers;
Expand Down Expand Up @@ -70,11 +71,7 @@ internal IEnumerable<RarFilePart> GetVolumeFileParts()
var part = CreateFilePart(lastMarkHeader!, fh);
var buffer = new byte[fh.CompressedSize];
part.GetCompressedStream().Read(buffer, 0, buffer.Length);
Comment = System
.Text
.Encoding
.UTF8
.GetString(buffer, 0, buffer.Length - 1);
Comment = Encoding.UTF8.GetString(buffer, 0, buffer.Length - 1);
}
}
break;
Expand Down
6 changes: 6 additions & 0 deletions tests/SharpCompress.Test/Rar/RarReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ public void Rar_Jpg_Reader()
[Fact]
public void Rar_Solid_Reader() => Read("Rar.solid.rar", CompressionType.Rar);

[Fact]
public void Rar_Comment_Reader() => Read("Rar.comment.rar", CompressionType.Rar);

[Fact]
public void Rar5_Comment_Reader() => Read("Rar5.comment.rar", CompressionType.Rar);

[Fact]
public void Rar5_Solid_Reader() => Read("Rar5.solid.rar", CompressionType.Rar);

Expand Down
Binary file added tests/TestArchives/Archives/Rar.comment.rar
Binary file not shown.
Binary file added tests/TestArchives/Archives/Rar5.comment.rar
Binary file not shown.