Skip to content

Commit

Permalink
memory leak fix (thanks to m^2)
Browse files Browse the repository at this point in the history
  • Loading branch information
inikep committed Dec 1, 2015
1 parent 3152a17 commit 4301f27
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
12 changes: 9 additions & 3 deletions lib/lz5frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ size_t LZ5F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf
dstPtr += errorCode;

if (prefs.compressionLevel >= (int)minHClevel) /* no allocation necessary with lz5 fast */
FREEMEM(cctxI.lz5CtxPtr);
LZ5_freeStreamHC(cctxI.lz5CtxPtr);

return (dstPtr - dstStart);
}
Expand Down Expand Up @@ -375,7 +375,10 @@ LZ5F_errorCode_t LZ5F_freeCompressionContext(LZ5F_compressionContext_t LZ5F_comp

if (cctxPtr != NULL) /* null pointers can be safely provided to this function, like free() */
{
FREEMEM(cctxPtr->lz5CtxPtr);
if (cctxPtr->prefs.compressionLevel < minHClevel)
FREEMEM(cctxPtr->lz5CtxPtr);
else
LZ5_freeStreamHC(cctxPtr->lz5CtxPtr);
FREEMEM(cctxPtr->tmpBuff);
FREEMEM(LZ5F_compressionContext);
}
Expand Down Expand Up @@ -410,7 +413,10 @@ size_t LZ5F_compressBegin(LZ5F_compressionContext_t compressionContext, void* ds
U32 tableID = (cctxPtr->prefs.compressionLevel < minHClevel) ? 1 : 2; /* 0:nothing ; 1:LZ5 table ; 2:HC tables */
if (cctxPtr->lz5CtxLevel < tableID)
{
FREEMEM(cctxPtr->lz5CtxPtr);
if (cctxPtr->prefs.compressionLevel < minHClevel)
FREEMEM(cctxPtr->lz5CtxPtr);
else
LZ5_freeStreamHC(cctxPtr->lz5CtxPtr);
if (cctxPtr->prefs.compressionLevel < minHClevel)
cctxPtr->lz5CtxPtr = (void*)LZ5_createStream();
else
Expand Down
14 changes: 8 additions & 6 deletions lib/lz5hc.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ int LZ5_alloc_mem_HC(LZ5HC_Data_Structure* ctx, int compressionLevel)
return 1;
}

void LZ5_free_mem_HC(LZ5HC_Data_Structure* statePtr)
void LZ5_free_mem_HC(LZ5HC_Data_Structure* ctx)
{
if (statePtr->chainTable) FREEMEM(statePtr->chainTable);
if (statePtr->hashTable) FREEMEM(statePtr->hashTable);
if (ctx->chainTable) FREEMEM(ctx->chainTable);
if (ctx->hashTable) FREEMEM(ctx->hashTable);
}


Expand Down Expand Up @@ -982,15 +982,17 @@ LZ5_streamHC_t* LZ5_createStreamHC(int compressionLevel)
FREEMEM(statePtr);
return NULL;
}

return (LZ5_streamHC_t*) statePtr;
}

int LZ5_freeStreamHC (LZ5_streamHC_t* LZ5_streamHCPtr)
{
LZ5HC_Data_Structure* statePtr = (LZ5HC_Data_Structure*)LZ5_streamHCPtr;
LZ5_free_mem_HC(statePtr);
free(LZ5_streamHCPtr);
if (statePtr)
{
LZ5_free_mem_HC(statePtr);
free(LZ5_streamHCPtr);
}
return 0;
}

Expand Down

0 comments on commit 4301f27

Please sign in to comment.