diff options
author | Eddy Jansson <eddy@klopper.net> | 2021-07-31 16:59:54 (GMT) |
---|---|---|
committer | Eddy Jansson <eddy@klopper.net> | 2021-07-31 17:05:29 (GMT) |
commit | 0a0922067fdb684c2c79169cc9b02118b905a6b9 (patch) | |
tree | aee8fce13258a6695b48cf03b39353b225c41f96 /lib | |
parent | 80e3e7034e84e7ff250d7f992ac7337551f800e8 (diff) | |
download | lz4-0a0922067fdb684c2c79169cc9b02118b905a6b9.zip lz4-0a0922067fdb684c2c79169cc9b02118b905a6b9.tar.gz lz4-0a0922067fdb684c2c79169cc9b02118b905a6b9.tar.bz2 |
Don't reuse state memory that's too small.
Ensure that the memory block we're trying
to reuse is large enough for the new state.
Fixes #974
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lz4frame.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 21dc47f..9069717 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -588,6 +588,16 @@ static void LZ4F_initStream(void* ctx, } } +static int ctxTypeID_to_size(int ctxTypeID) { + switch(ctxTypeID) { + case 1: + return LZ4_sizeofState(); + case 2: + return LZ4_sizeofStateHC(); + default: + return 0; + } +} /*! LZ4F_compressBegin_usingCDict() : * init streaming compression AND writes frame header into @dstBuffer. @@ -610,7 +620,9 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, /* cctx Management */ { U16 const ctxTypeID = (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) ? 1 : 2; - if (cctxPtr->lz4CtxAlloc < ctxTypeID) { + int requiredSize = ctxTypeID_to_size(ctxTypeID); + int allocatedSize = ctxTypeID_to_size(cctxPtr->lz4CtxAlloc); + if (allocatedSize < requiredSize) { /* not enough space allocated */ FREEMEM(cctxPtr->lz4CtxPtr); if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { |