Giter Club home page Giter Club logo

Comments (6)

mjebrahimi avatar mjebrahimi commented on July 26, 2024 1

Relevant issues:
MiloszKrajewski/K4os.Compression.LZ4#42
MiloszKrajewski/K4os.Compression.LZ4#43

from easycompressor.

MiloszKrajewski avatar MiloszKrajewski commented on July 26, 2024 1

@sgf @mjebrahimi

Basically this is a problem with the LZ4 itself and has nothing to do with EasyCompressor and I do not know the reason for this.

I disagree. There is no problem with LZ4 (in this respect), the problem is in your library.

I found that sometimes LZ4 throws an exception when decompressing large files.
In this case, depending on the file size, one of these errors may occur:

  • OverflowException: Arithmetic operation resulted in an overflow
  • ArgumentOutOfRangeException: Specified argument was out of the range of valid values

No, it does not. It throws in EasyCompressor.LZ4\LZ4Compressor.cs. To be very specific, in:

protected override byte[] BaseDecompress(byte[] compressedBytes)
{
    var target = new byte[compressedBytes.Length * 255].AsSpan();
    int decoded = LZ4Codec.Decode(compressedBytes, target);
    return target.Slice(0, decoded).ToArray();
}

you allocate compressed length * 255 bytes. This mean that ~8.1 MB (8421504 B to be exact) file is enough to allocate over 2GB of memory. Everything over it will cause excaption: ArgumentOutOfRangeException or OverflowException (depending on compiler settings, what fails first: multiplication arithmetic overflow or memory allocation).

...and the StackTrace of original report confirms that:

StackTrace:
at EasyCompressor.LZ4Compressor.BaseDecompress(Byte[] compressedBytes) in D:\a\EasyCompressor\EasyCompressor\src\EasyCompressor.LZ4\LZ4Compressor.cs:line 42
at EasyCompressor.BaseCompressor.Decompress(Byte[] compressedBytes) in D:\a\EasyCompressor\EasyCompressor\src\EasyCompressor\Compressor\BaseCompressor.cs:line 80
at ConsoleAppCompressTest.Program.Main(String[] args) in G:\Src\my\M3\Mir3Apps\ConsoleAppCompressTest\Program.cs:line 15

(the problem is in EasyCompressor.LZ4Compressor.BaseDecompress

Just try this snippet:

int x = 8421505 * 255;

from easycompressor.

MiloszKrajewski avatar MiloszKrajewski commented on July 26, 2024 1

So is it right to always use the stream method since the original source size is usually unknown at decompression time?

Not really. There are several ways to make size known. Like storing it? While compressing you can allocate 4 byes more and actually store length in 4 first bytes (or 4 last bytes if you wish).

Or you can use LZ4Pickler which was designed to do exactly that?

from easycompressor.

mjebrahimi avatar mjebrahimi commented on July 26, 2024

I found that sometimes LZ4 throws an exception when decompressing large files.
In this case, depending on the file size, one of these errors may occur:

  • OverflowException: Arithmetic operation resulted in an overflow
  • ArgumentOutOfRangeException: Specified argument was out of the range of valid values

Basically this is a problem with the LZ4 itself and has nothing to do with EasyCompressor and I do not know the reason for this.
I have opened this issue to follow this case.

The MiloszKrajewski/lz4net repo is no longer maintained and has been replaced by MiloszKrajewski/K4os.Compression.LZ4 and This LZ4Compressor just is a wrapper on it and nothing else.

from easycompressor.

mjebrahimi avatar mjebrahimi commented on July 26, 2024

@MiloszKrajewski OK, you're right.
So is it right to always use the stream method since the original source size is usually unknown at decompression time?

from easycompressor.

mjebrahimi avatar mjebrahimi commented on July 26, 2024

Great point.
So I refactored the code to store the actual size length (4 bytes) at first.
Thanks, @MiloszKrajewski.

from easycompressor.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.