inikep / lizard

Lizard (formerly LZ5) is an efficient compressor with very fast decompression. It achieves compression ratio that is comparable to zip/zlib and zstd/brotli (at low and medium compression levels) at decompression speed of 1000 MB/s and faster.
Other
644 stars 40 forks source link

Lizard_createStream doesn't initialize all members #12

Closed GregSlazinski closed 7 years ago

GregSlazinski commented 7 years ago

After calling: if(Lizard_stream_t *lizard=Lizard_createStream(Mid(compression_level, LIZARD_MIN_CLEVEL, LIZARD_MAX_CLEVEL))) I get some uninitialized values, like lizard->end which caused crashes in subsequent call to 'Lizard_compress_continue'

After I changed the code to zero the context, the crashes disappeared

Lizard_stream_t* Lizard_initStream(Lizard_stream_t* ctx, int compressionLevel) 
{
..
if (!ctx)
    {
        size_t size = sizeof(Lizard_stream_t) + hashTableSize + chainTableSize + LIZARD_COMPRESS_ADD_BUF + LIZARD_COMPRESS_ADD_HUF;
        ctx = (Lizard_stream_t*)malloc(size);
       if (!ctx) { printf("ERROR: Cannot allocate %d MB (compressionLevel=%d)\n", (int)(size)>>20, compressionLevel); return 0; }
        memset(ctx, 0, size); // <----------HERE
        LIZARD_LOG_COMPRESS("Allocated %d MB (compressionLevel=%d)\n", (int)(size)>>20, compressionLevel); 
        ctx->allocatedMemory = size;

Also your codes calculated this "sizeof(Lizard_stream_t) + hashTableSize + chainTableSize + LIZARD_COMPRESS_ADD_BUF + LIZARD_COMPRESS_ADD_HUF" several times, it's better to compute this once into a helper variable first.

Also I suggest removing all "printf" calls from your library, and either use some error code result enums, or an optional callback which accepts the messages.

inikep commented 7 years ago

Greg, thanks for you bug report. I fixed the issue (the init flag was not set) with this commit: https://github.com/inikep/lizard/commit/71c2dd365d668c98d4d8afa7c5d38dc5f2788edc